aspose file tools*
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
Author

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)?


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

Joined: Oct 13, 2005
Posts: 40061
    
  28
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: http://aspose.com/file-tools
 
subject: Multi-dimensional arrays