Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Variable with string as index or smt...

 
Ranch Hand
Posts: 179
  • 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.
 
Sheriff
Posts: 7023
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also Hashtable, HashMap can be used depending on your requirement
 
Ranch Hand
Posts: 823
  • 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
Boost this thread!