gpt4 book ai didi

java - Java 中的锯齿状数组 : Converting char[][] and ArrayList>

转载 作者:行者123 更新时间:2023-12-02 08:58:05 25 4
gpt4 key购买 nike

如何转换以下内容,使其本质上是 ArrayList 的锯齿状嵌套 ArrayList,每行/列都是字符列表?

   //Following excerpt from https://www.geeksforgeeks.org/jagged-array-in-java/
int r = 5;
//Need this to have capacity to hold list of chars (eg, each row/col index can be empty, //have 1 char, or multiple chars
char toFill = new Variable(" ");

// Declaring 2-D array with 5 rows... need it to be ArrayList<ArrayList<char>>
char matrix[][] = new Variable[r][];


// Creating a 2D array such that first row
// has 1 element, second row has two
// elements and so on.
for (int i=0; i<matrix.length; i++) {
matrix[i] = new char[i + 1];
}

// Initializing array
int count = 0;
for (int i=0; i<matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = toFill;
}
}

最佳答案

实际上列表是动态结构,您不需要像数组等静态结构那样预先定义它们的大小。他是一个示例程序,直接将锯齿状数组转换为锯齿状列表列表。我尽力保留尽可能多的结构,以便您更容易理解。代码看起来仍然有点“数组式”(我实际上不喜欢它,因为它有点不自然),但我希望你明白这个想法。

package de.scrum_master.stackoverflow.q60367936;

import java.util.ArrayList;
import java.util.List;

/**
* Demonstrate 2-D jagged array/list such that first row has 1 element,
* second row has two elements and so on.
*/
class Main {
private static void jaggedArray() {
int r = 5;

// Declaring 2-D array with 5 rows
int arr[][] = new int[r][];

// Creating a 2D array such that first row has 1 element, second row has two elements and so on.
for (int i = 0; i < arr.length; i++)
arr[i] = new int[i + 1];

// Initializing array
int count = 0;
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;

// Displaying the values of 2D Jagged array
System.out.println("Contents of 2D Jagged Array");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}

private static void jaggedArrayList() {
int r = 5;

// Declaring 2-D list of lists
List<List<Integer>> arr = new ArrayList<>();

// Adding empty sub list to main list
for (int i = 0; i < r; i++)
arr.add(new ArrayList<Integer>());

// Initializing 2-D list
int count = 0;
for (int i = 0; i < r; i++)
for (int j = 0; j <= i; j++)
arr.get(i).add(count++);

// Displaying the values of 2D Jagged list
System.out.println("Contents of 2D Jagged ArrayList");
for (List<Integer> list : arr) {
for (Integer i : list)
System.out.print(i + " ");
System.out.println();
}
}

public static void main(String[] args) {
jaggedArray();
System.out.println("\n------------------------------\n");
jaggedArrayList();
}
}

控制台日志:

Contents of 2D Jagged Array
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14

------------------------------

Contents of 2D Jagged ArrayList
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14

如您所见,数组和列表变体都会产生相同的结果。无论您使用List<List<Integer>>List<List<Char>>对于算法来说并不重要。

关于java - Java 中的锯齿状数组 : Converting char[][] and ArrayList<ArrayList<char>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60367936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com