java - Assign a name to every element of an array? -
is possible assign name every element of array? example in array myarr[5]
, can give myarr[0]
name "first", myarr[1]
"second" , on? , if not, might feasible way achieve similar result?
use map
:
map<string, whatever> yourmap = new hashmap<>(); yourmap.put("first", something); yourmap.put("second", somethingelse);
then, elements out:
system.out.println(yourmap.get("first")); // prints something.tostring() system.out.println(yourmap.get("second")); // prints somethingelse.tostring() // etc.
but if you're going use "first" , "second" keys, you're better off using array. maps useful when it's more convenient access things using arbitrary objects rather simple integers keys.
Comments
Post a Comment