0

I want to declare anonymous arraylist in java so that on the nature of constructor list can be initialize with any type of object. For example:

    ArrayList<Anonymous> list;

    public AdapterChatWindow(Activity act, ArrayList<CommentData> list, String extras) {        
        this.act = act;     
        this.list = list;
    }

    public AdapterChatWindow(Activity act, ArrayList<ChatHistory> list) {       
        this.act = act; 
        this.list = list;
    }

Is this possible? Any alternate Solution? Is this possible with list type data structure?

Jaroslaw Pawlak
  • 5,538
  • 7
  • 30
  • 57
Mark Henry
  • 255
  • 3
  • 8

3 Answers3

2

You can use a generic type:

public class AdapterChatWindow<T>
{
    ArrayList<T> list;

    public AdapterChatWindow(Activity act, ArrayList<T> list, String extras)
// ....

Then use it like this:

AdapterChatWindow<Foo> acw = new AdapterChatWindow<Foo>(...);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

You can go for generic for example..

{List<? extends "your super class"> list;}

now you can initialize your list with any of the subclass of "your super class"

Mitul Sanghani
  • 230
  • 1
  • 11
0

You could just use ArrayList<Object> list but you'll have to carefully cast your array items when you want to do anything with them.

ci_
  • 8,594
  • 10
  • 39
  • 63
  • If you are going for `ArrayList list`, you might as well skip the parametrization: `ArrayList list` – ortis Mar 12 '15 at 10:41
  • I think i have found a better answer myself by declaring arraylist like this ArrayList> list; in java. Thanks but for your help – Mark Henry Mar 12 '15 at 10:42