7

I'm wondering if arrays in Java could do something like this:

int[] a = new int[10];
a["index0"] = 100;
a["index1"] = 100;

I know I've seen similar features in other languages, but I'm not really familiar with any specifics... Just that there are ways to associate values with string constants rather than mere numeric indexes. Is there a way to achieve such a thing in Java?

Shog9
  • 156,901
  • 35
  • 231
  • 235
Fugogugo
  • 4,460
  • 10
  • 36
  • 50

6 Answers6

18

You can't do this with a Java array. It sounds like you want to use a java.util.Map.

Map<String, Integer> a = new HashMap<String, Integer>();

// put values into the map
a.put("index0", 100); // autoboxed from int -> Integer
a.put("index1", Integer.valueOf(200));

// retrieve values from the map
int index0 = a.get("index0"); // 100
int index1 = a.get("index1"); // 200
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • ah , so it's like that.. hashmap huh? thank you :) – Fugogugo Feb 14 '11 at 14:37
  • 2
    @Fugogugo: `HashMap` is an implementing class; `Map` is the interface. I recommend coding against the `Map` interface, which in this case means declaring your variable as a `Map` and not a `HashMap`. – Matt Ball Feb 14 '11 at 14:39
4

I don't know a thing about C++, but you are probably looking for a Class implementing the Map interface.

Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39
POSIX_ME_HARDER
  • 772
  • 9
  • 22
  • aaah,,, so it do exist. the Map class. thanks. I'm new in java :) – Fugogugo Feb 14 '11 at 14:37
  • @Fugogugo Since you are new to Java, it is worth noting that you can't just stick anything into a map. If you use a TreeMap whatever your keys need to implement the Comparable interface. If you use HashMap, make sure you have the hashCode and equals methods overridden on the keys being placed into the map. The default implementations of hashCode and equals will suffice in many cases, however. – Matt Wonlaw Mar 30 '11 at 16:06
  • wait, what ?? so confusing comment – Rishiraj Purohit Dec 22 '16 at 07:38
2

What you need is java.util.Map<Key, Value> interface and its implementations (e.g. HashMap) with String as key

king_nak
  • 11,313
  • 33
  • 58
2

To store things with string keys, you need a Map. You can't use square brackets on a Map. You can do this in C++ because it supports operator overloading, but Java doesn't.

There is a proposal to add this syntax for maps, but it will be added for Java 8 at the earliest.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
0

java does not have associative arrays yet. But instead you can use a hash map as an alternative.

lahiru madhumal
  • 1,185
  • 2
  • 12
  • 30
0

Are you looking for the HashMap<k,v>() class? See the javadocs here.

Roughly speaking, usage would be:

HashMap<String, int> a = new HashMap<String,int>();
a.put("index0", 100);

etc.

  • 2
    Maps cannot use primitives. It would have to be a `Map`; likewise, it's typically better practice to declare it as a `Map` and not a `HashMap`. – Matt Ball Feb 14 '11 at 14:40
  • I already try it. yes it can't use primitive. but the concept is right. thank you. :) – Fugogugo Feb 14 '11 at 14:43