java - Using a HashMap and an ArrayList to store variable names and values. How to use for creating new objects? -
i have hashmap stores name of object variable , values need used object.
the object creation be:
room [key] = new room([value1]);
below hashmap:
public static map<string, list<string>> makeroom(string[] room){ map = new hashmap<string, list<string>>(); int roomnumber = integer.parseint(room[1]); list<string> valsetone = new arraylist<string>(); valsetone.add(room[2]); valsetone.add(room[3]); valsetone.add(room[4]); valsetone.add(room[5]); map.put("r"+roomnumber, valsetone); return map; } }
how use produce new object? example, have 3 keys: r1, r2, r3
. how can produce code:
room r1 = new room(1); room r2 = new room(2); room r3 = new room(3);
thank in advance.
to answer question, can this:
constructor roomconstructor = room.class.getconstructor(integer.class); room r1 = roomconstructor.newinstance(integer.parseint(map.get("r1"))); room r2 = roomconstructor.newinstance(integer.parseint(map.get("r2"))); room r3 = roomconstructor.newinstance(integer.parseint(map.get("r3")));
the class object lets grab constructor , create new instance out of it, provided pass in correct parameters.
Comments
Post a Comment