简体   繁体   中英

Return int[][] array

this might be an ridiculous question, but I don`t make it. I got two arrays this Type:

String[] string{"A", "B", "C"} and int[][] array {{1, 2, 3},{1, 2, 3}}

and wish to get this output:

A 1 1

B 2 2

C 3 3

I have almost tried every possible for loop, but it does not get in that order. Thanks a lot for helping me!

Try this:

// INIT ARRAYS
String[] abcArray = {"A", "B", "C"};
int[][] otherArray = {{1, 2, 3},{1, 2, 3}};

// LOOP ABC ARRAY
for(int x = 0; x < abcArray.length; x++){

    // PRINT
    system.out.println(abcArray[x]+otherArray[0][x]+otherArray[1][x]);

}
    String[] string = {"A", "B", "C"};
    int[][] array = {{1, 2, 3},{1, 2, 3}};

    for (int i = 0; i < string.length; i++) {
        System.out.print(string[i]);
        for (int j = 0; j < array.length; j++) {
            System.out.print(" " + array[j][i]);
        }
        System.out.println();
    }

This one will result in your desired output, as long as your string array has the same length as every int array in your 2d array. You can adjust the for-loops based on your use-case, but you'll get the idea.

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