• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable with string as index or smt...

 
Juhan Voolaid
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
 
Gerome Kawa
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also Hashtable, HashMap can be used depending on your requirement
 
Julian Kennedy
Ranch Hand
Posts: 823
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic