嵌套for循环以在Java中打印2D数组的输出

时间:2014-09-13 14:26:10

标签: java multidimensional-array nested-loops

我一直在努力解决以下问题:

我正在尝试使用嵌套for循环和二维数组打印下面的输出。

int[][] outputArray = {

{1,2,3,4,5,6,7,8,9,10},

{11,12,13,14,15,16,17,18,19,20},

{21,22,23,24,25,26,27,28,29,30},

{31,32,33,34,35,36,37,38,39,40},

{41,42,43,44,45,46,47,48,49,50},

{51,52,53,54,55,56,57,58,59,60},

{61,62,63,64,65,66,67,68,69,70},

{71,72,73,74,75,76,77,78,79,80}

};

这是我的数组代码似乎是正确的:

    public ExerciseTwo() {
    myArray1 = new int[8][10];

    for (int i = 0; i < myArray1.length; i++) {
        for (int j = 0; j < myArray1[i].length; j++) {
            myArray1[i][j] = (i * myArray1[i].length) + j + 1;
        }// end inner loop
    }// end outer loop
}// end constructor

现在我遇到了以下嵌套循环的几个问题:

    public void printArrayStatement() {
    System.out.print("int[][] outputArray = {");

    for (int i = 0; i < myArray1.length; i++) {

        if (myArray1.length >= 1)
            System.out.print("\n" + "{" + myArray1[0][0]); //I am trying to remove the initial comma here but my logic is wrong. It is printing 1 first on each line.

        for (int j = 0; j < myArray1[i].length; j++) {

            System.out.print("," + myArray1[i][j]);

        }

    }
    System.out.println("};");
}// end method

我似乎也无法弄清楚如何在每一行的末尾获得}。我认为if语句是必要的,但我无法弄清楚代码!

4 个答案:

答案 0 :(得分:2)

以下代码按要求运行。 :

System.out.println("int[][] outputArray = {");   //int[][]outpuArray = {
for (int i = 0; i < myArray1.length; i++) {
    System.out.print("{");                       //{
    int j; 
    for (j = 0 ; j < myArray1[i].length - 1; j++) {
        //1, 2,...9,    i.e not last one.
        System.out.print(myArray1[i][j]+", ");   //1, 2,...9, then terminate
        //Not used if to check for last one because it would increase time complexity
    }
    System.out.print(myArray1[i][j]+"}");        //10} 
    if(i!=myArray1.length -1){                   
        System.out.println(", ");                //, only if it is not last one
    }
}
System.out.println("\n}");

输出

int[][] outputArray = {
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, 
{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
{41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, 
{51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, 
{61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, 
{71, 72, 73, 74, 75, 76, 77, 78, 79, 80}
}

答案 1 :(得分:0)

看起来你正在创建一个可以简单解决的复杂代码...如果你不在最后打印逗号,如果你&#不打印它39;最后

for (int i = 0; i < myArray1.length; i++) {
  for (int j = 0; j < myArray1[i].length; j++) {
    // here print out myArray1[i][j]
    if (j != myArray1[i].length - 1) {
      // here print out comma if j is not == myArray1[i].length - 1
    }
  }
  System.out.println();
}

另一种选择是使用java.util.Arrays.toString(...)

我不确定您为什么要打印{}符号。你确定你需要它们吗?

答案 2 :(得分:0)

你的内循环从零开始:

   for (int j = 0; j < myArray1[i].length; j++) {

你应该

   for (int j = 1; j < myArray1[i].length; j++) {

至于右括号,只需在内循环后打印,但仍在外循环内:

  System.out.println("}");
}
System.out.println("};");

答案 3 :(得分:0)

内循环之后我们打印'}'并检查它是否只打印了最后一行,如果没有,那么我们也需要打印逗号。

以下是代码:

for (int i = 0; i < myArray1.length; i++) {
    if (myArray1.length >= 1)
        System.out.print("\n" + "{" + myArray1[0][0]);
    for (int j = 0; j < myArray1[i].length; j++) {
        System.out.print("," + myArray1[i][j]);
    }

    // Print ',' if we are not at the end.
    System.out.print("}"+(i==myArray1.length-1?"":",")); 
}
System.out.println("\n};");