简体   繁体   中英

Nested for loop to print output of 2D array in Java

I have been struggling with the following problem:

I am trying to print the below output using nested for loops and two dimensional arrays.

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}
};

Here is my code for the array which seems to be correct:

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

Now I am having several issues with the nested loop below:

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

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

I also can't seem to figure out how to get the }, at the end of each line. I think an if statement is necessary but I can't figure out the code!

The following code is working as required:

System.out.println("int[][] outputArray = {");   //int[][] outputArray = {
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}");

Output:

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}
}

It looks like you're creating a complex bit of code that can be solved simply... print the comma if you're not at the end, and don't print it if you're at the end

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();
}

Another option is to use java.util.Arrays.toString(...)

I'm not sure why you are trying to print the { and } symbols. Are you sure that you need them?

Your inner loop is starting from zero:

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

You should have

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

As for the closing brace, just print it after the inner loop, but still inside the outer loop:

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

After the inner loop we print '}' and we check to see if it just printed the last line, if not then we need to print comma also.

Here is the code:

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};");

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