Q:

How to retrieve a value in ArrayList where it contains two different object type arraylist..?

I have two ArrayList of types ArrayList<ObjectType1> and ArrayList<ObjectType2>.
Then I have created another ArrayList<Object> as object is super class of every object type.
ArrayList<Object1> obj1 = new ArrayList<Object1>();

ArrayList<Object2> obj2 = new ArrayList<Object2>();

These two object class has its own specific getters and setters method.
ArrayList<Object> obj3 = new ArrayList<Object>();

obj3.add(obj1);
obj3.add(obj2);

Now when I am accessing obj3 I want to fetch obj1 and obj2's getters and setters.??
Can any one please help for a solution..???

A:

You can do this things:
ArrayList<MyClass1> obj1 = new ArrayList<MyClass1>();
ArrayList<MyClass2> obj2 = new ArrayList<MyClass2>();

ArrayList<Object> obj3 = new ArrayList<Object>();

obj3.add(obj1); 
obj3.add(obj2);

for (int = 0 ; i < obj3.size() ; i++) {
    Object obj = obj3.get(i);
    if (obj instanceof MyClass1) {
        MyClass1 cls1 = (MyClass1)obj;
        cls1.get()..//You getters and setters
    }
    if (obj instanceof MyClass2) {
        MyClass2 cls2 = (MyClass2)obj;
        cls2.get()..//You getters and setters
    }
}

Or you can do something smarter and create an interface or Abstract Class/Base Class
public Interface Getable {
    void getAttrbiute();
}

And than just implement in MyClass1, MyClass2.
In this case you can run over the list without the casting an the instaceof check
ArrayList<MyClass1> obj1 = new ArrayList<MyClass1>();
ArrayList<MyClass2> obj2 = new ArrayList<MyClass2>();

ArrayList<Getable> obj3 = new ArrayList<Getable>();

obj3.add(obj1); 
obj3.add(obj2);

for (int = 0 ; i < obj3.size() ; i++) {
   Getable objGetable = obj3.get(i);
   objGetable.getAttrbiute();
 }

Hope that helps..

