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.

0

4 Answers 4

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]
Sign up to request clarification or add additional context in comments.

Comments

2

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

Good luck!

2 Comments

lol! too bad the answer is already provided...
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!
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.

Comments

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]

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.