gpt4 book ai didi

java - 创建大小未定的二维锯齿状数组

转载 作者:行者123 更新时间:2023-12-02 04:49:34 24 4
gpt4 key购买 nike

对于我的程序,我正在制作一个二维锯齿状整数数组,内部每个数组的长度取决于用户的输入。

我们可以这样说:

int seq [] [] = new int [M] [];
for(int i = 0; i < M; i++){
seq[i] = new int [N[i]];

数组的总数(M)和每个数组的N个数组的长度取决于用户的输入。有没有办法可以做到这一点,以便生成的二维数组可以由类内的任何方法使用?

谢谢。

最佳答案

您可以将其设置为实例变量数组,并在某些 init 方法或构造函数中对其进行初始化。

public class Test{
int[][] array;

public void initialize() {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
array = new int[m][n];
}

public void processArray() {
if(array != null) {
//process array
}
}
}

关于java - 创建大小未定的二维锯齿状数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29352154/

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