| Author |
Variable with string as index or smt...
|
Juhan Voolaid
Ranch Hand
Joined: Nov 18, 2003
Posts: 179
|
|
Hi I wonder if in Java there is possible to create vector, array ,list or maybe another new object type that is similar to an array in PHP language. What i mean is i want to make ... let's say an array of images, where index of that array element is a string ... just like in PHP. example to describe what i want: So with what type of objects is it possible to do, I'm sure it's not possible with arrays.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
Maps
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Gerome Kawa
Ranch Hand
Joined: Dec 05, 2002
Posts: 61
|
|
|
Also Hashtable, HashMap can be used depending on your requirement
|
 |
Julian Kennedy
Ranch Hand
Joined: Aug 02, 2004
Posts: 823
|
|
Just to expand on Dirk's very succinct response, what you're describing is an associative array and, as you suspect, there's nothing exactly like that in Java. You can achieve the same functionality by using any of the Collections classes, from java.util, that implement the Map interface (Maps! ). These classes maintain collections of name/value pairs, i.e. associations. Before you read on you may like to try this out for yourself; it's pretty straightforward. So, using a Map your sample code might look like this: The cast to (Image) is required as get() returns an Object. Jules
|
 |
 |
|
|
subject: Variable with string as index or smt...
|
|
|