4

I recently started mutltidimensional int arrays in java. Before that one dimensional arrays had sufficed.

To print those I used System.out.println(Arrays.toString(myarray));

But it won't work with multi dimensional arrays, it does the same thing as it does when you try to print a one dimensional array directly only many times.

4
  • Iterate through each dimension and use Arrays.toString() Commented Dec 30, 2014 at 18:32
  • 2
    Arrays.deepToString() is better though Commented Dec 30, 2014 at 18:34
  • I tried it and it worked, but it gave me everything in one line, which made it hard to read. Commented Dec 30, 2014 at 18:35
  • @JigarJoshi Can I iterate through the second dimension? Commented Dec 30, 2014 at 19:03

4 Answers 4

10

You'll need to iterate through it.

The easiest way to do so is to use the provided Arrays.deepToString() which searches for arrays within the array. Note that it, like Arrays.toString(), is only available in Java 1.5+ although I certainly hope that you are using Java 5 by now.

For example:

int[][] myArray = { { 1, 2, 3 }, { 4, 5, 6 } };
System.out.println(Arrays.deepToString(myArray));

Alternatively, you can iterate yourself which also allows for more customization with the style in which you choose to print.

For example:

int[][] myArray = { { 1, 2, 3 }, { 4, 5, 6 } };
for(int[] arr : myArray)
    System.out.println(Arrays.toString(arr));
Sign up to request clarification or add additional context in comments.

4 Comments

don't you have to put brackets after myArray?
@omnitic I'm not sure what you mean. Did I make a typo?
Upvoted, but I would reverse the order of the two parts of this answer -- mention Arrays.deepToString first, since for most cases it's probably preferable.
I preferred iterating myself since deep gave me everything in one line
0

Try something like:

int number[][] = { {1, 2, 3}, {1}, {2}};
for (int i = 0; i < number.length; i++)
    System.out.println(Arrays.toString(number[i]));

Comments

0

Try Commons Lang 3's ArrayUtils toString method:

   @Test
   public void multiArrayToString(){
      int[] a = {1,2,3};
      int[] b = {4,5,6};
      int[][] c = {a, b};
      String aCommonsLangToString = ArrayUtils.toString(a);
      String bCommonsLangToString = ArrayUtils.toString(b);
      String cCommonsLangToString = ArrayUtils.toString(c);
      System.out.println(aCommonsLangToString);
      System.out.println(bCommonsLangToString);
      System.out.println(cCommonsLangToString);
   }

Output:

{1,2,3}
{4,5,6}
{{1,2,3},{4,5,6}}

2 Comments

Do we really need Commons Lang dependency for a simple method which Java already has?
Good point, the Arrays.deepToString() method would help without the extra dependency, as posed by @pbabcdefp above. Using this the last line of output would be [[1, 2, 3], [4, 5, 6]].
0

you can easily iterate trough the array like this:

if (yourArray != null) {
    // Where object type means the type of your array.
    for (ObjectType subArray : yourArray) {
        // If this method can't receive nulls, then validate that subArray isn't null
        System.out.println(Arrays.toString(subArray));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.