Is there a way to use names as indexes in arrays? like
ary["name"]="ABCD";
ary["age"]="20";
System.out.println(ary["name"] + " " + ary["age"]);
Is there a way to use names as indexes in arrays? like
ary["name"]="ABCD";
ary["age"]="20";
System.out.println(ary["name"] + " " + ary["age"]);
Use a Map instead.
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
In your case, your snippet would be:
Map<String,String> ary = new HashMap<String,String>();
ary.put("name","ABCD");
ary.put("age", "20");
System.out.println(ary.get("name") + " " + ary.get("age"));
Maybe you can use a HashMap
Example:
HashMap<String,String> map = new HashMap<String,String>();
map.put("hello","hi");
System.out.println(map.get("hello"));
output: hi
It'd be a dictionary at that point. From: How do you create a dictionary in Java?:
Map<String, String> map = new HashMap<String, String>();
map.put("dog", "type of animal");
System.out.println(map.get("dog"));