简体   繁体   中英

How to retrieve a value in ArrayList<Object> 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..???

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

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.

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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM