ArrayList<ArrayList> liste = new ArrayList<ArrayList>();

eclipse saying :

ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized

The list should be containing some strings and some integers. So what I should write is ArrayList<ArrayList<MyClass>> to get rid off the warning. The problem is I dont know how to write MyClass since all ArrayList<MyClass> may not contain the same number of element :

[ [france paris 10000 jean jacques pierre] [usa washington 120000 james jack] ]
[ [string1 string2 int string3 string4 string5] [string1 string2 int string3 string4] ]

As you can see, 2nd arraylist has 1 member less than 1st. So how to write MyClass?

An ArrayList "doesn't care" how long it is. It is dynamic.

I know. It works perfectly without giving a type to Arraylist but I want to get rid of the warnings

The number of elements in the ArrayList has nothing to do with the type of those elements. You only need to specify the type.

If need to type the contained ArrayLists but those can contain any type you can use <ArrayList<ArrayList<?>> , but if you are using mixed-type collections you might want to stop and make sure you know why. There are cases in which they are useful, but in general you intend to use the objects in a collection in some consistent manner (else why did you group them together at all?) and perhaps you should really find a common type for them to share. Often that type is a base class type or an interface which all of your elements share so tey can be operated upon in a uniform manner.

Im making a P2P application uing JavaRMI so Im storing the lisf of shared files in networks this way :
lets say filea and fileb are shared,

[ [CRCa LENGTHa IDa PATHa ID2a PATH2a]
[CRCb LENGHTb IDb PATHb]]

- CRC is the file's checksum
- LENGTH is the file's length
- ID is the id of a peer proxy or @ip if you prefer)
- PATH is the path of file shared
- If there are multiples peers sharing the same file then ID2 is the proxy of peer2, PATH2...etc
- 2 sources for filea

You would be much better server by creating a class to hold those attributes then, rather than lumping them into an ArrayList and having to discern the various properties by index.

> If need to type the contained ArrayLists but those can contain any type you can use
> <ArrayList<ArrayList<?>>
That way he would neither be able to instantiate nor insert elements into the given list.

A better way would be to do something like:

public class Lists {
    public static void main(String args[]) {
        /* First approach */
        List<List<? extends Object>> lists = new ArrayList<List<? extends Object>>();
        List<Object> listOne = new ArrayList<Object>();
        listOne.add("hello"); listOne.add(666);
        lists.add(listOne);
        List<Integer> listTwo = new ArrayList<Integer>();
        listTwo.add(1); listTwo.add(2);
        lists.add(listTwo);
        List<String> listThree = new ArrayList<String>();
        listThree.add("listry"); listThree.add("kaboom");
        lists.add(listThree);
        System.out.println(Arrays.asList(lists));
        
        /* Second approach */
        List<Klass> kLists = new ArrayList<Klass>();
        Klass klassOne = new Klass();
        klassOne.addToCharList("hello").addToNumList(34);
        kLists.add(klassOne);
        Klass klassTwo = new Klass();
        klassTwo.addToCharList("hi").addToCharList("bye").addToNumList(666);
        kLists.add(klassTwo);
        System.out.println(Arrays.asList(kLists));
    }
}

class Klass {
    private List<Number> numList;
    private List<CharSequence> charList;
    
    public Klass() {
        numList = new ArrayList<Number>();
        charList = new ArrayList<CharSequence>();
    }
    
    public Klass addToNumList(Number num) {
        numList.add(num);
        return(this);
    }
    
    public Klass addToCharList(CharSequence ch) {
        charList.add(ch);
        return(this);
    }
    
    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder(1024);
        for(int i = 0, max = numList.size(); i < max; ++i) {
            buf.append(numList.get(i) + " ");
        }
        for(int i = 0, max = charList.size(); i < max; ++i) {
            buf.append(charList.get(i) + " ");
        }
        return(buf.toString());
    }
}

I find the second approach to be much more manageable than the first one.

commented: Good catch. Thanks. +5

> If need to type the contained ArrayLists but those can contain any type you can use
> <ArrayList<ArrayList<?>>
That way he would neither be able to instantiate nor insert elements into the given list.

Ah, thanks for pointing that out. Since I've never used the 'unknown' type in that manner, I didn't realize the limitation. Good to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.