0

I have the following as two ways to declare a two dimensional String array. I am being asked to find another way but I don't think there is any other way. Can someone please give me some advice.

    String[][] a = new String[20][20]
    String a[][] = new String[20][20]

Thanks

8
  • IMHO maybe you should get a book with Java basics. @siegi has posted the other way to declare arrays. Commented Jun 18, 2012 at 15:59
  • @LuiggiMendoza I cannot think of a Java basics book that would contain that sort of information. And if it would contain that information, it would be a bad book... ;) Commented Jun 18, 2012 at 16:02
  • Well I have books but I could not see any way. Sorry but I did try. Commented Jun 18, 2012 at 16:04
  • 1
    @LuiggiMendoza Actually, yes. It's very confusing and I can not see any pro's on using that notation. The only reason to actually store this in my head is to impress someone else with useless java knowledge. :D But as always: If you show me a good use for it, I will take everything back. ;) Commented Jun 18, 2012 at 16:14
  • 1
    @brimborium extracted from the book: "We can also declare multidimensional arrays, which are in fact arrays of arrays. This can be done in the following manner: String[][][] occupantName; // recommended [breakline] String[] ManagerName []; // yucky, but legal". Commented Jun 18, 2012 at 16:35

1 Answer 1

6
String[] a[] = new String[20][20]

Edit: Note that this works with methods too:

private String[] a()[]
{
    return new String[20][20];
}

Also note that I do not recommend this notation as it is unclear and confusing!

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

4 Comments

Wow, did not know that. What a mess. :D
@brimborium Indeed… I didn't know that either until some weeks ago… :-D
Initialization, not declaration.
@user1458374 Sorry, I don't understand what you would like to say with that. The question is about declaration of two dimensional arrays…

Your Answer

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