4

I need to make an int array using Strings instead of ints. EX: int["number2"] = 0; instead of int[2] = 0;

Does anyone know how to do this?

Thanks for your time.

4 Answers 4

5

you could use a HashMap - see here for more info!

Sign up to request clarification or add additional context in comments.

1 Comment

Or more generally, any kind of Map.
3

Java doesn't support associative arrays, but you could use a HashMap:

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key1", 25);
map.put("key2", 4589); 

map.get("key1") will return 25.

Comments

2

You are not looking for an array but for an associative array.

In Java, in practice, every class that implements Map can be used as an associative container, since they can map keys to values (TreeMap<K,V>, HashMap<K,V>, and so on)

Comments

1

This syntax looks very like a map in Groovy, In Java, you could use something like a Map<String, Integer>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.