java - Insert dynamic data into 2D array of strings -
i trying make catalog shop. have 2d array:
string childelements[][] = new string[][];
i want add data array. data array:
arraylist<string> names = new arraylist<string>();
names
can vary depending on input get.
for example if furniture store, categories tables
, chairs
etc.
i have categories stored in array like:
string groupelements[] = {"tables", "chairs"};
and names
contains: {"wood", "metal", "4-seater", "6-seater"}
so, want childelements
array reflect like:
childelements = ["chairs"]["wood", "metal"] ["tables"]["4-seater"]["6-seater"]
so how insert data serve needs?
i stick array of arrays , not go list
or hashmap
architecture depends on that.
as @dude suggested use hashmap, organise things easier. in case category key , value array.
// create map store map<string, list<string>> map = new hashmap<string, list<string>>(); // create list 1 , store values list<string> chairs = new arraylist<string>(); chairs.add("wood"); chairs.add("metal"); // create list 2 , store values list<string> tables = new arraylist<string>(); tables.add("4-seater"); tables.add("6-seater"); // put values map map.put("chairs", chairs); map.put("tables", tables); // iterate , display values system.out.println("fetching keys , corresponding [multiple] values"); (map.entry<string, list<string>> entry : map.entryset()) { string key = entry.getkey(); list<string> values = entry.getvalue(); system.out.println("key = " + key); system.out.println("values = " + values); }
Comments
Post a Comment