2

Can Java use a String as an index array key?

Example:

array["ab"] = "ab";

1 Answer 1

4

No you cannot. Array index must be integer.

And if you want to pair them up, look at Map, where you can use String as a key and value Map<String,String>.

Map<String,String> map = new HashMap<String,String>();
map.put("ab","ab"); //adding key-value;

String value map.get("ab"); //gives you "ab"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.