15

How to print multi-dimensional array using for-each loop in java? I tried, foreach works for normal array but not work in multi-dimensional array, how can I do that? My code is:

class Test
{
   public static void main(String[] args)
   {
      int[][] array1 = {{1, 2, 3, 4}, {5, 6, 7, 8}};
      for(int[] val: array1)
      {
        System.out.print(val);
      }
   } 
}
5
  • Can you give an example of the output you'd like to see? Commented Oct 6, 2012 at 14:42
  • What's not working? Do you get an error? If so post it. If it doesn't output what you expect, post what it does print and what you expect it to. Commented Oct 6, 2012 at 14:42
  • sorry i cant upload photo but out put is: G:\santoo>javac Test.java Test.java:6: error: incompatible types for(int[][] val: array1) ^ required: int[][] found: int[] 1 error Commented Oct 6, 2012 at 14:46
  • @SanthoshKumar What version of java are you using? G:\santoo>java -version? Commented Oct 6, 2012 at 14:51
  • Please try my solution with serialization (below) Commented Oct 6, 2012 at 14:53

4 Answers 4

13

Your loop will print each of the sub-arrays, by printing their address. Given that inner array, use an inner loop:

for(int[] arr2: array1)
{
    for(int val: arr2)
        System.out.print(val);
}

Arrays don't have a String representation that would, e.g. print all the elements. You need to print them explicitly:

int oneD[] = new int[5];
oneD[0] = 7;
// ...

System.out.println(oneD);

The output is an address:

[I@148cc8c

However, the libs do supply the method deepToString for this purpose, so this may also suit your purposes:

System.out.println(Arrays.deepToString(array1));
Sign up to request clarification or add additional context in comments.

Comments

8

If you just want to print the data contained in the int array to a log, you can use

Arrays.deepToString

which does not use any for loops.

Working Code.

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
      int[][] array = {{1, 2, 3, 4}, {5, 6, 7, 8}};
        System.out.println(Arrays.deepToString(array));
   } 
}

Output

[[1, 2, 3, 4], [5, 6, 7, 8]]

Comments

4

This is a very general approach that works in most languages. You will have to use nested loops. The outer loop accesses the rows of the array, while the inner loop accesses the elements within that row. Then, just print it out and start a new line for every row (or choose whatever format you want it to be printed in).

for (int[] arr : array1) {
  for (int v : arr) {
    System.out.print(" " + v);
  } 
  System.out.println();
}

1 Comment

@MartijnPieters Absolutely agree, that seems to be an old answer of mine. ;) Hope that fixed it.
2

The current output would look something like:

[I@1e63e3d
...

which shows the string representation for an integer array.

You could use Arrays.toString to display the array content:

for (int[] val : array1) {
   System.out.println(Arrays.toString(val));
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.