gpt4 book ai didi

java - 在java上创建各种矩阵

转载 作者:行者123 更新时间:2023-12-01 11:38:40 26 4
gpt4 key购买 nike

我正在尝试创建一个 Java 应用程序,它将数字作为输入并创建各种矩阵。

做这个的最好方法是什么?

我已经做了这个,然后我尝试通过数组来制作它。

public class main {

public static void main(String[] args) {
int max = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Number of matrix?");
max = scan.nextInt();

int[] matrius = new int[max];
int[][] matriu = new int[2][2];

matrius[0] = matriu[2][2];
matrius[1] = matriu[2][2];

for(int i = 0; i < matrius.length; i++){
matrius[i] = i;
}

for(int i = 0; i < matrius.length; i++){
System.out.println(matrius[i]);
}

}

}

谢谢!

最佳答案

创建一个列表来保存所有 2 x 2 的二维矩阵。列表具有用户输入的初始容量,即最大值。

现在循环并创建二维数组并将它们添加到列表中。

    public static void main(String[] args) {
int max = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Number of matrix?");
max = scan.nextInt();

List<int[][]> allMatrices = new ArrayList<int[][]>(max);

for(int i = 0; i < max; i++){
int x[][]=new int [2][2];
allMatrices.add(x);
}


// To acces the 2 D arrays

foreach(int [][] x : allMatrices){


for(int i=0;i<x.length;i++)
{
for(int j=0;j<x[i].length ;j++)
{
// do some operation on x[i][j]
}
}

}

关于java - 在java上创建各种矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29735814/

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