File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Variable with string as index or smt... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable with string as index or smt..." Watch "Variable with string as index or smt..." New topic

Variable with string as index or smt...

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179

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

Joined: Dec 10, 2001
Posts: 7023

[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.

I agree. Here's the link:
subject: Variable with string as index or smt...
It's not a secret anymore!