简体   繁体   English

如何在Java变量中存储2d数组的行?

[英]How can I store a row of a 2d array in a variable in Java?

    int array[][] = {{2, 3, 5, 8, 10, 2, 12, 5, 7}, {2, 4, 5, 6, 10, 4, 36, 6, 9}, {4, 5, 5, 10, 2, 9, 4, 7, 12}};

    System.out.println("Seite1 " + " Seite2 " + " Seite3 " + " Dreieckstyp");
    dreieckstyp(array);

}

static void dreieckstyp(int arrays[][]) {
    for (int zeilen = 0; zeilen < arrays.length; zeilen++) {
        for (int spalten = 0; spalten < arrays[zeilen].length; spalten++) {
        }
    }

    int a = arrays[0][1];

Instead of just storing the 2 I would like to store in a the whole row: 2, 3, 5, 8, 10, 2, 12, 5, 7 我不希望只存储2,而是要整行存储: 2, 3, 5, 8, 10, 2, 12, 5, 7
I already tried using int a [] = array[0]; 我已经尝试使用int [] = array [0]; but that mess up with the code and I cant use the if statement: if ((a * a + b * b == c * c)) System.out.println(a + " \\t\\t " + b + " \\t\\t " + c + " \\t " + " rechtwinkelig"); 但这弄乱了代码,我不能使用if语句:if((a * a + b * b == c * c))System.out.println(a +“ \\ t \\ t” + b +“ \\ t \\ t“ + c +” \\ t“ +” rechtwinkelig“); because the operator * cannot be applied to int[]. 因为运算符*不能应用于int []。 I want to prove the condition a² + b² = c² 我想证明条件a²+b²=c²

First of all, a twodimensional array doesn't really exist in Java, it is just an array of arrays. 首先,二维数组在Java中并不真正存在,它只是一个数组数组。

If you want to store a list into a variable, you'll need a to be an array: 如果要将列表存储到变量中,则需要a作为数组:

int[] a = array[0];

Then if you want to operate on each value of a , you can just walk over all elements, like this: 然后,如果要对a每个值进行运算,则可以遍历所有元素,如下所示:

for (int aa : a) {
    // Do something with aa
}

By the way, you should give your variables descriptive names, the use of variables like array and a is not quite clear to future readers. 顺便说一句,您应该给变量起一个描述性的名称,对arraya类的变量的使用对将来的读者来说还不太清楚。

Just use : 只需使用:

 int a[] = array[0];

Outputs 输出

[2, 3, 5, 8, 10, 2, 12, 5, 7]

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

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