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

11,097

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}
}
Share:
11,097
Mac
Author by

Mac

Updated on June 04, 2022

Comments

  • Mac
    Mac almost 2 years

    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!

  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 9 years
    Please elaborate why the inner loop should start at 1. I must be confused on what the OP is asking.
  • Marko Topolnik
    Marko Topolnik over 9 years
    To skip the first item which was already explicitly printed inside the if block above it.
  • Mac
    Mac over 9 years
    Thanks for the response. Yeah I need the brackets. The output will copied into another method to initialize an array so the output has to be exact.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 9 years
    @macattack: this still strikes me as very strange. Your other method would work much better if you passed the actual array itself and not some String representation that only makes sense to a compiler. Color me very confused.
  • Mac
    Mac over 9 years
    There is still an issue with the if statement. It is printing 1 as the first element in each line instead of increasing by 10. For example, line two should begin with 11, line 3 with 21 and so on...
  • Mac
    Mac over 9 years
    Also, originally I had myArray1.length; in the outer loop. You changed it to myArray1[i].length; - why did you make that change? I did it and I am getting an ArrayIndexOutOfBoundsException.
  • Marko Topolnik
    Marko Topolnik over 9 years
    So print myArray[i][0]. Also check myArray[i].length >= 1. Btw I never mention your outer loop conditions.
  • Mac
    Mac over 9 years
    I used your if statement and I added the brackets using separate print statements as mentioned above and it is working now.
  • Mac
    Mac over 9 years
    Could you please explain what is happening in your code? Why do you need two print statements that contain (myArray1[i][j])? Should that not make have the array elements print twice?
  • afzalex
    afzalex over 9 years
    Tried to explain it with comments @macattack. Tell me if you still have any doubt.
  • Mac
    Mac over 9 years
    Thanks. That makes sense to me now.
  • Mac
    Mac over 9 years
    It's a unique situation. It was required by my prof. We have to test the accuracy of the printed output by copying and pasting the output into another method.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 9 years
    @macattack: then that's a very good reason to do something -- because the boss or instructor wants it done! I retract my questioning of the motivation behind this code.