简体   繁体   中英

Can I store string in object array by using this string as index?

Can Java use a String as an index array key?

Example:

array["ab"] = "ab";

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM