File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Multi-dimensional arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multi-dimensional arrays" Watch "Multi-dimensional arrays" New topic

Multi-dimensional arrays

Chris Montgomery
Ranch Hand

Joined: Jan 14, 2004
Posts: 141
I'm in the process of converting some PHP logic over to java.

One of the methods puts data into a multi-dimensional array:

As shown above, the questions array is multi-dimensional. $questions[] is an int and $questions[][] is a String.

The multi-dimensional arrays I've used only allow int.

Are there any Java objects out there equipped for my scenario (Array[int][string] = some value)?

[ May 11, 2007: Message edited by: Chris Montgomery ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
Array[int][string]?? No.

Several ways you could do it:

Two separate arrays

Two separate data structures

An object which includes the number and String together I think the last is the best solution.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Surely the thing that's important here is that text data is being used to key the "array" in PHP. Java arrays are not "associative arrays" and can only have integer indexes.

In Java, you should use Collections in preference to arrays, in most code. To mimic a PHP associative array with text keys, you might like a HashMap<String, Object>. (Replace the Object by the type of the value, if it is more specific than Object).

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: Multi-dimensional arrays
It's not a secret anymore!