Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multi-dimensional arrays

 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic