I have this object Object1 defined as public Object1(String item, List<Object2> obj2) in a class (whilst Object2 is defined in another class as public Object2(String item1, String item2))
Next, in the main program, I’m creating a list of Object1 objects:
public static List<Object1> obj1 = new List<Object1>();
List<Object2> obj2 = new List<Object2>();:
obj1.add("first", obj2);
obj1.add("second", obj2);
obj1.add("third", obj2);
Next I want to add some objects into obj2 for every obj1 specifically:
obj1[0].Obj2.Add(new Object2("first","first");
obj1[0].Obj2.Add(new Object2("first","second");
obj1[1].Obj2.Add(new Object2("second","first");
obj1[1].Obj2.Add(new Object2("second","second");
obj1[2].Obj2.Add(new Object2("third","first");
obj1[2].Obj2.Add(new Object2("third","second");
Now the problem is that, for example, obj[0].Obj2.Count is 6, and it’s the same for obj[1].Obj2.Count and so on. How should I populate obj2 so that each Obj2 would be different depending on obj1[]? For example obj1[0].Obj2 different from obj1[1].Obj2 and obj1[0].Obj2.Count to be 2.
Please excuse me if I didn’t explain my problem properly or didn’t write all the necessary details. Thank you for your answers in advance.
If I understand you well, you should have a different list for each Object1
Have you considered using a Map ?