Penergy
Penergy

Reputation: 1

two arraylist merge with the same father class in JAVA

I have a question. Right now, I have three classes.

public C(){}

public A extends C {}

public B extends C {}

What I want to do is to create two arraylist:

ArrayList<A> list_A;
ArrayList<B> list_B;

Finally, I want to merge this two arraylists with the same father class.

I check the forum: list_A.addAll(list_B) is used with the same class object.

Upvotes: 0

Views: 53

Answers (1)

Eran
Eran

Reputation: 393781

You can merge them in a container of the super type :

ArrayList<C> list_C = new ArrayList<C>();
list_C.addAll(list_A);
list_C.addAll(list_B);

Upvotes: 1

Related Questions