如何在Java中动态初始化多维数组

huangapple go评论66阅读模式
英文:

How to initialize a multidimensional array on the go in java

问题

我有一个多维数组,其中第二维的长度不总是相同的。

是否可能初始化一个数组,您首先可以像这样创建第一个部分

我正在使用这个数组来定义矩阵中每行的长度。

int[] config = {4,4,4,3,2,4};
boolean[][] array = new boolean[config.length][];

然后在循环遍历数组并使用所需的长度创建子数组?

for(int i = 0; i < config.length; i++)
    array[i] = new boolean[config[i]];

附注:我不会使用ArrayList。

英文:

I have a multidimensional array where the second dimension is not always the same length.

Is it possible to initialize an array where you first create the first part like this

I am using this array to define the length of each row in the matrix.

int[] config = {4,4,4,3,2,4};
boolean[][] array = new boolean[config.lenght][];

and then after loop over the array and create the subarrays with the desired length?

for(int i = 0; i&lt;config.length; i++)
    boolean[i][] = new boolean[config[i]];

PS: I will not use ArrayList

答案1

得分: 1

public static int[][] generate(int[] config) {
    int[][] arr = new int[config.length][];
    Random random = new Random();

    for (int row = 0; row < arr.length; row++) {
        arr[row] = new int[config[row]];

        for (int col = 0; col < arr[row].length; col++)
            arr[row][col] = random.nextInt(100);
    }

    return arr;
}

Output:

[
  [97, 80, 78, 88],
  [31, 97, 34, 39],
  [67, 92, 89, 0],
  [29, 96, 72],
  [68, 77],
  [7, 65, 68, 51]
]
英文:
public static int[][] generate(int[] config) {
    int[][] arr = new int[config.length][];
    Random random = new Random();

    for (int row = 0; row &lt; arr.length; row++) {
        arr[row] = new int[config[row]];

        for (int col = 0; col &lt; arr[row].length; col++)
            arr[row][col] = random.nextInt(100);
    }

    return arr;
}

Output:

[
  [97, 80, 78, 88],
  [31, 97, 34, 39],
  [67, 92, 89, 0],
  [29, 96, 72],
  [68, 77],
  [7, 65, 68, 51]
]

答案2

得分: 0

可以将任何单个的 D 数组赋值给相同类型的 2D 数组。

int[] s = {1,2,3,4,5,6};

int[][] twoD = new int[s.length][];

for (int i = 0; i < v.length; i++) {
   twoD[i] = new int[]{s[i]}; // `s` 数组中一个元素的新数组
}

for (int[] s : twoD) {
  System.out.println(s);
}

输出

[1]
[2]
[3]
[4]
[5]
[6]

或者

System.out.println(Arrays.deepToString(twoD));

输出

[[1], [2], [3], [4], [5], [6]]
英文:

You can assign any single D array to a 2D array of the same type.


int[] s = {1,2,3,4,5,6};

int[][] twoD = new int[s.length][];

for (int i = 0; i &lt; v.length; i++) {
   twoD[i] = new int[]{s[i]}; // new array of one element of the `s` array
}

for ( int[] s : twoD) {
  System.out.println(s);
}

Prints

[1]
[2]
[3]
[4]
[5]
[6]

or

System.out.println(Arrays.deepToString(twoD));

Prints

[[1], [2], [3], [4], [5], [6]]

</details>



# 答案3
**得分**: 0

以下是翻译好的部分:

我不完全理解这个问题,但我有一个类似这样的解决方案建议。

```java
int[] config = {4,4,4,3,2,4};
int[][] array = new int[config.length][config.length];
for(int i = 0 ; i<config.length ; i++){
    array[i][0] = config[i];
}

数组输出:

400000
400000
400000
300000
200000
400000
英文:

I do not fully understand the question, but I have a suggestion for a solution like this.

            int[] config = {4,4,4,3,2,4};
            int[][] array = new int[config.length][config.length];
            for(int i = 0 ; i&lt;config.length ; i++){
                array[i][0] = config[i];
            }

array output:

400000
400000
400000
300000
200000
400000

答案4

得分: 0

你还可以使用流:

int[] config = {4, 4, 4, 3, 2, 4};
boolean[][] myArr = Arrays.stream(config).mapToObj(i -> new boolean[i]).toArray(boolean[][]::new);

for (boolean[] b : myArr) {
    System.out.println(Arrays.toString(b));
}
英文:

You can also use streams:

int[] config = {4,4,4,3,2,4};
boolean[][] myArr = Arrays.stream(config).mapToObj(i -&gt; new boolean[i]).toArray(boolean[][]::new);
    
for(boolean[] b : myArr){
    System.out.println(Arrays.toString(b));
}

huangapple
  • 本文由 发表于 2020年10月19日 06:22:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定