0

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..???

2
  • what you have tried ? Commented May 27, 2014 at 11:33
  • what do you mean by "I want to fetch obj1 and obj2's getters and setters"? Commented May 27, 2014 at 11:33

3 Answers 3

3

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..

Sign up to request clarification or add additional context in comments.

2 Comments

when I am doing obj3.size, I am getting a definite size but when I am doing obj3.get(i) in the next step its returning null, Why is this so?
maybe in the obj1 , obj2 you inserted null in one of the nodes?
0

if it is a simple object of few extensions scope u can use instanceof for example

class ObjectType1 {
    String getData1() {
        return "data 1";
    }
}

class ObjectType2 {
    String getData2() {
        return "data 2";
    }
}
private String getData(Object obj) {
    if (obj instanceof ObjectType1)
        return ((ObjectType1) obj).getData1();
    else
        return ((ObjectType2) obj).getData2();
}

But for good programming practise try using interfaces or abstraction a basic interface implementation might look like

interface DataHolder{
    String getData();
}
class ObjectType1 implements DataHolder {
    String getData1() {
        return "data 1";
    }
    public String getData(){
        return getData1();
    }
}

class ObjectType2  implements DataHolder {
    String getData2() {
        return "data 2";
    }
    public String getData(){
        return getData2();
    }
}
List<DataHolder> dataSet = new ArrayList<DataHolder>();

private void print() {
    dataSet.add(new ObjectType1());
    dataSet.add(new ObjectType2());
    for (DataHolder iHolder : dataSet){
        System.out.println(iHolder.getData());
    }
}

Comments

0

You'll have identified that you pull objects from obj3 as Object, rather than their individual types.

You can either:

  1. check the type of each object extracted via instanceof and cast appropriately before accessing the getters. This is very nasty.
  2. Or, (much better) implement a common interface containing the appropriate getters. This interface would be implemented by every object in your obj3 list, and that list would be defined as ArrayList<MyInterface>, where MyInterface defines your getters.

Fundamentally this is a modelling issue. By putting everything into a list of Objects, you're saying that all you know about these objects is that they're Objects. Instead you need to leverage polymorphism around your solution.

1 Comment

can you give an example for your second option?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.