0

I have a two dimensional array

 public class TwoDimensions {

public static void main(String[] args){
    String[][] names = new String[3][3]; 
        names[0][0] = "Joe";
        names[1][1] = "Jane";
        names[2][2] = "Herkimer";
    for(String[] output:names){
        System.out.println(names);
    }
    }
}

How can I print all of its content. For location [0][1] I have nothing. So it should print null or something.

Some Java Guy
  • 4,992
  • 19
  • 71
  • 108

4 Answers4

2

It seems like homework question, So I would give you a hint...try to read about nested for-loops.

Good luck!

TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • lol! too bad the answer is already provided... – VDP Apr 04 '12 at 12:51
  • Yeah I saw that! Damn, I wish I had stackoverflow when I learnt these things. :( I screwed my head and couple of times bang my head on wall in university! – TeaCupApp Apr 04 '12 at 12:54
2

If you want to print this as a grid you can use

for (String[] row : names)
    System.out.println(Arrays.toString(row));

prints

[Joe, null, null]
[null, Jane, null]
[null, null, Herkimer]
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

If you don't want to print a null value, then check it with an if condition. If it is null, then continue.

You can use the continue statement.

user unknown
  • 35,537
  • 11
  • 75
  • 121
Vicky
  • 1,215
  • 6
  • 22
  • 45
0

The approach as @doNotCheckMyBlog mentioned is good, but from what was asked only with the title. Google identifies with this title also n-dimensional arrays, so that I will post here a solution to print n-dimensional arrays for everybody who needs it:

This method is constructed from two functions. One prints the array, and the other one creates an array object from primitives and normal objects. (Workaround of the java.lang.ClassCastException if you do (Object[])(new int[]{1,2,3}))

public static String printNdimArray(Object[] in) {
    String ret = "[";
    for(Object obj : in ) {
        if( obj.getClass().isArray() )
            ret += printNdimArray( createArrayFromArrayObject(obj) );
        else
            ret += obj.toString();
        ret += ", ";
    }
    ret = ret.substring(0, ret.length() - 2);
    return ret +"]";
}

The other functions casts object or primitive arrays into normal object arrays that can be used to call the above function again, till to the leaves of the original array tree. (Original code from: https://stackoverflow.com/a/6427453)

public static Object[] createArrayFromArrayObject(Object o) {
    if(!o.getClass().getComponentType().isPrimitive())
        return (Object[])o;

    int element_count = Array.getLength(o);
    Object elements[] = new Object[element_count];

    for(int i = 0; i < element_count; i++)
        elements[i] = Array.get(o, i);          
    return elements;
}

In extraordinary large arrays the structure is clearly not clear anymore, but that can fixed with some tweaks. With a small amount of objects in the array it looks like that:

out: [Mad Max: Fury Road, 7200, [600, 3600, 4000], [EN], [DE, EN, FR], sci_fi]

It also does a great job with displaying 1 dimensional arrays:

out: [FR, EN, DE, CA]
Community
  • 1
  • 1
Georg Friedrich
  • 183
  • 2
  • 12