0

I'm trying to iterate over an ArrayList of ArrayLists - but somehow everything fails on me and I don't understand the error message.

The error is:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.ArrayList

I've tried using a regular for(int i; i < lists.length; i++) but get the same error. All I want to do is check if any of the ArrayLists in "lists" contains the integer "v".

public static boolean listsContains(ArrayList<ArrayList<Integer>> lists, int v) {
    boolean b = false;

    for (ArrayList<Integer> list : lists) {
        if (list.contains(v)) {
            b = true;
        } else {
            b = false;
        }
    }
    return b;
}

The actual line that causes the error is the "for (ArrayList list"...

Edited: For clarity I edited in the code with more declarative generics (which works just as little as the first code I posted unfortunately).

Edit2: Ok so it's somehow not the method itself that causes the problem so upon request here's the rest of the code that populates these lists. The code is not done but I got caught with this problem while finishing it.

public static void main(String[] args) {
    Graph g = DataSource.load();

    ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();

    for(int i = 0; i < g.numberOfVertices(); i++) {
        if(!(listsContains(lists, i))) {    // add list if node is unlisted (since after first iteration one entire network is found)
            listsCreate(lists, i);
        }

        Iterator it = g.adj(i).iterator();  // create iterator for current node's edges

        if (!(it.hasNext())) {   // node has no edges
            listsCreate(lists, i);
        } else {                // node has edges, iterate through them
            while(it.hasNext()) {
                Edge current = (Edge) it.next();
                if(!(listsContains(lists, current.to))) {   // unlisted node
                    int index = listsIndexOf(lists, current.from);
                    findNetwork(g, lists.get(index), current.to);
                } else {
                    continue;       // node already listed
                }
            }
        }
    }

    System.out.println("Number of connected graphs: " + lists.size());

}   // Main
8
  • show us you method call. Commented Dec 12, 2015 at 12:56
  • 2
    Also, rethink about your logic: it creates a useless iterator. And it loops over all the arraylists, but only keep the result for the last one. And use generics: It should be an ArrayList<ArrayList<Integer>>. Or even better, a List<List<Integer>>. Commented Dec 12, 2015 at 12:58
  • Post all the relevant code, and the complete stack trace of the exception. The problem is not in the posted code (although it has several other problems). Your outer list doesn't contain ArrayList instances. Commented Dec 12, 2015 at 13:03
  • You're correct it's from error solving, I've tried so many things now. Been at it for an hour :( Commented Dec 12, 2015 at 13:13
  • Post all the relevant code, and the complete stack trace of the exception. The problem is not in the posted code. It's in the code that generates that list of lists, and which actually generates a list containing integers. Commented Dec 12, 2015 at 13:17

2 Answers 2

1

You did not specify the inner ArrayList's components' type. And from your log I can tell that it contains Integers:

public static boolean listsContains(ArrayList<ArrayList<Integer>> lists, int v) {

    for (ArrayList<Integer> list : lists) {
        if (list.contains(v)) 
            return true;
    }

    return false; // No inner arrayList contains 'v'
}

EDIT:

or using Java 8 :

public static boolean listsContains(ArrayList<ArrayList<Integer>> lists, int v) {
     return lists.stream().anyMatch(list -> list.contains(v));
}
Sign up to request clarification or add additional context in comments.

7 Comments

Or better: return lists.stream().anyMatch(list -> list.contains(v));. But the lack of generics doesn't explain the error.
@JBNizet in deed, it's much better & faster. It's just I'm a Java 8 noob, so... thanks for the better alternative !
your code won't change anything at runtime. It will just cause compile error at the line with method call. Generics are applied only at compile-time, have no business at runtime
@Sparta it's probably not faster, but it's shorter and expresses the intent more clearly.
Wow. I actually had missed that one of my methods tried calling the problematic method with an ArrayList<Integer> instead of ArrayList<ArrayList<Integer>> and the IDE did'nt even warn me about it. Thanks for your help in helping me find the problem
|
1

Because you test:

list.contains(v)

list is of type ArrayList without inside type

v is int

replace your ArrayList by ArrayList< Integer >

1 Comment

This did not solve the problem, also edited op to reflect this. Thanks for input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.