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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With