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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -