简体   繁体   English

返回int [] []数组

[英]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}} String[] string{"A", "B", "C"}int[][] array {{1, 2, 3},{1, 2, 3}}

and wish to get this output: 并希望获得以下输出:

A 1 1 1 1

B 2 2 B 2 2

C 3 3 C 3 3

I have almost tried every possible for loop, but it does not get in that order. 我几乎尝试了所有可能的for循环,但顺序不尽相同。 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. 只要您的字符串数组与2d数组中的每个int数组具有相同的长度,这将产生所需的输出。 You can adjust the for-loops based on your use-case, but you'll get the idea. 您可以根据用例调整for循环,但是您会明白的。

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

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