1

so, I'm trying to print multidimensional arrays with print statements

output of row 0: 2 3 6 8 10 12 14 16,
output of row 1:4 6 8 10 12 14 16 18 
and output of row 2: 6 8 10 12 14 16 18 20

Code:

public static void main(String [] args) {

    int [][] array = {{2,4,6,8,10,12,14,16},{4,6,8,10,12,14,16,18},{6,8,10,12,14,16,18,20}};

      for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
      }    
}
5
  • 3
    Unclear what problem you are having. Are you missing a System.out.println(); after the inner loop? Commented Dec 21, 2018 at 8:01
  • 3
    And your issue is? Commented Dec 21, 2018 at 8:02
  • 1
    Simpler, but not with exactly the same output: Arrays.asList(array).forEach(a -> System.out.println(Arrays.toString(a))); Commented Dec 21, 2018 at 8:06
  • Please, are you stating the observed output or the output that you want in your question? Please also state the other so we can know what the difference and therefore what your issue is. Commented Dec 21, 2018 at 8:09
  • 1
    Please be more clear on what exactly are you asking for ? With the above question it becomes absolute nightmare to guess what you could probably be asking ! Commented Dec 21, 2018 at 9:22

3 Answers 3

3

Note that the difference between print and println:

println(String x):

Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

print(String s):

Prints a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

So println add an addtional line separator for you compared with print

Reference:

https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println()

Sign up to request clarification or add additional context in comments.

Comments

2

There are multiple ways to handle this. I suggest for-each loop to print the arrays as below:

public static void main(String[] args) {
    int[][] array = {{2, 4, 6, 8, 10, 12, 14, 16}, {4, 6, 8, 10, 12, 14, 16, 18}, {6, 8, 10, 12, 14, 16, 18, 20}};
    int i=1;
    for (int[] row : array)
    {
        System.out.println("output of row " +i +Arrays.toString(row));
        i++;
    }
}
}

1 Comment

@OleV.V. Thanks for your suggestion. Really appreciate it.
1

You can change your code like this and get your output

public class Main {

    public static void main(String[] args) {
        int[][] array = {{2, 4, 6, 8, 10, 12, 14, 16}, {4, 6, 8, 10, 12, 14, 16, 18}, {6, 8, 10, 12, 14, 16, 18, 20}};
        for (int i = 0; i < array.length; i++) {
            System.out.print("output of row " + i + ": ");
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

    }
}

2 Comments

how can I add print statements to this code, eg. row 0 output is 2,4,6,8,10,12,14,16 and row 1 is 4,6,8,10,12,14,16,18.
@sammy1180 Its the same code. Only difference is I have added System.out.print("output of row " + i + ": "); before your inner loop and added System.out.println(); after your inner loop

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.