java - Java 中的锯齿状数组:转换 char[][] 和 ArrayList>
问题描述
如何转换以下内容,使其本质上是 ArrayLists 的锯齿状嵌套 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
如您所见,数组和列表变量都产生相同的结果。无论您使用 aList<List<Integer>>
还是 aList<List<Char>>
都与算法无关。
推荐阅读
- php - 如何在 laravel 视图中将数组显示为表格格式
- javascript - 用虚线连接的堆积柱点
- nginx - 使用 Nginx 入口控制器在 413 状态码上添加自定义响应
- android - 创建android条码阅读器以获取数据库信息
- python - 为什么在我的 Django 视图中查询“green bay”时“green”或“bay”不起作用?
- java - 从 JPanel 调整 JFrame 的大小
- google-drive-api - 有没有办法在不下载文件的情况下获取存储在 Google 相册中的图片的哈希值?
- r - 如何纠正多元线性回归模型的异方差性
- python - 如何根据与输入字符串共享的特征从数据集中获取值
- javascript - 访问不同端口时,js fetch 失败并出现 net::ERR_SSL_PROTOCOL_ERROR