简体   繁体   English

如何在 java 中使用 m(rows)、n(cols) 初始化 2D 数组?

[英]How do i initialize a 2D Array with m(rows), n(cols) in java?

I wanted to initialize a 2D arraylist of size 2n-1(rows), n(cols)我想初始化一个大小为 2n-1(rows), n(cols) 的二维数组列表

    int num = 3; 
    // declare an arrayList of ArrayLists or 2D ArrayList
    ArrayList<ArrayList<Integer>> list =  
           new ArrayList<ArrayList<Integer>>(num);

The above syntax is to create 2D ArrayList of size Num*Num.上述语法是创建大小为 Num*Num 的 2D ArrayList。

In arrays we initialize with M*N, similarly can we do with ArrayLists?在我们用 M*N 初始化的数组中,我们可以用 ArrayLists 做类似的事情吗? if yes, how?如果是,如何?

I want syntax to initialize with M*N.我想用 M*N 初始化语法。

Unlike an Array which must be initialized to a fixed length, an ArrayList can grow dynamically, you don't have to worry about M and N to initialize it.与必须初始化为固定长度的 Array 不同,ArrayList 可以动态增长,您不必担心MN来初始化它。 You worry about that when you want to fill the ArrayList and if filling the collection to M and N is your specific requirement then do so after the ArrayList has been declared.当您想要填充 ArrayList 并且如果将集合填充到MN是您的特定要求时,您会担心,那么在声明 ArrayList 之后执行此操作。 So, ArrayList<ArrayList<Integer>> list = new ArrayList<>();所以, ArrayList<ArrayList<Integer>> list = new ArrayList<>(); is all you need.是你所需要的全部。

There are a number of ways to fill the ArrayLists to your desired M (rows) and N (columns).有多种方法可以将 ArrayLists 填充到所需的M (行)和N (列)。 Here is a relatively decent visual way to do it.这是一种相对不错的视觉方式。

ArrayList<ArrayList<Integer>> list = new ArrayList<>();
ArrayList<Integer> innerList;
int m = 20;  // Rows
int n = 10;  // Columns 
int incrementer = 1;

for (int i = 0; i < m; i++) {
    innerList = new ArrayList<>();
    for (int j = 0; j < n; j++) {
        innerList.add(j + incrementer);
    }
    list.add(innerList);
    incrementer ++;
}
    
// Now to display (print) the 2D ArrayList into the Console Window:
for (ArrayList<Integer> inner : list) {
    for (Integer ints : inner) {
        System.out.printf("%-3s ", ints);
    } 
    System.out.println();
}

The console should display:控制台应显示:

1   2   3   4   5   6   7   8   9   10  
2   3   4   5   6   7   8   9   10  11  
3   4   5   6   7   8   9   10  11  12  
4   5   6   7   8   9   10  11  12  13  
5   6   7   8   9   10  11  12  13  14  
6   7   8   9   10  11  12  13  14  15  
7   8   9   10  11  12  13  14  15  16  
8   9   10  11  12  13  14  15  16  17  
9   10  11  12  13  14  15  16  17  18  
10  11  12  13  14  15  16  17  18  19  
11  12  13  14  15  16  17  18  19  20  
12  13  14  15  16  17  18  19  20  21  
13  14  15  16  17  18  19  20  21  22  
14  15  16  17  18  19  20  21  22  23  
15  16  17  18  19  20  21  22  23  24  
16  17  18  19  20  21  22  23  24  25  
17  18  19  20  21  22  23  24  25  26  
18  19  20  21  22  23  24  25  26  27  
19  20  21  22  23  24  25  26  27  28  
20  21  22  23  24  25  26  27  28  29  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM