Question

I have an ArrayList like : ArrayList<ArrayList<String>> test

for(int i = 0; i < test.size(); i++) {
    ArrayList temp = test.get(i);
}

It turns out now, that I cannot do Arrays.deepToString(temp.toArray()) since temp is an Object and not < ArrayList< String>>.

Also, I am doing temp1.contains(temp.get()) temp1 and temp2 are actually objects with the same values < ArrayList< String>>. They even have some common ArrayLists within them. However, due to this Object problem, this does not work.

Is there any way by which I can basically return an ArrayList or so instead of an object so that I can get the .contains() method to work. As I said... the ArrayList within temp has the same number of columns, some with the same values.

Please help me out.

Was it helpful?

Solution

ArrayList temp = test.get(i);

should be

ArrayList<String> temp = test.get(i);

OTHER TIPS

If you parameterize the ArrayList and pass over the return type to the toArray() it should work fine.

for(int i = 0; i < test.size(); ++i){
    ArrayList<String> temp = test.get(i);
    temp.toArray(new String[]{});
}

The toArray() now returns a String[]

    ArrayList<ArrayList<String>> test=new ArrayList<>();
    ArrayList<String> list=new ArrayList<>();
    list.add("a");
    list.add("b");
    list.add("c");
    test.add(list);
    ArrayList<String> temp=new ArrayList<>();
    for(ArrayList<String> arr:test) {
        temp = arr;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top