简体   繁体   中英

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
I already tried using int a [] = 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"); because the operator * cannot be applied to int[]. I want to prove the condition a² + b² = c²

First of all, a twodimensional array doesn't really exist in Java, it is just an array of arrays.

If you want to store a list into a variable, you'll need a to be an array:

int[] a = array[0];

Then if you want to operate on each value of a , you can just walk over all elements, like this:

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.

Just use :

 int a[] = array[0];

Outputs

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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