Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Java use String as an index array key? (ex: array["a"]=1;)

Tags:

java

arrays

map

Can Java use a String as an index array key? Example:

array["a"] = 1; 
like image 289
Jaynova Avatar asked Aug 04 '11 14:08

Jaynova


People also ask

Can an array index be a string in Java?

No, that would be a Map in Java. (The type would be Map<String,Integer> .)

Can you use a string as an array index?

Actually, it has very much to do with the question (specifically, yes, you CAN use a string as an index, but not in the obvious way that the original querier wants).

Can Strings be treated as arrays in Java?

String class split(String regex) can be used to convert String to array in java. If you are working with java regular expression, you can also use Pattern class split(String regex) method.


1 Answers

No.

To do something like this, you have to use a Map.

Map<String, Integer> aMap = new HashMap<String, Integer>(); aMap.put("a" , Integer.valueOf(1)); 
like image 102
Kal Avatar answered Oct 05 '22 23:10

Kal