60
public class MyGraph<V,E> extends SparseMultigraph<V,E>{
    private ArrayList<MyNode> myNodeList;

    public MyNode getNode(int nodeId){
        myNodeList = new ArrayList<MyNode>();
        myNodeList = (ArrayList<MyNode>)this.getVertices();
        int i;

The following are the error msg:

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

Can anyone help?

2
  • Loop through and just add the nodes. Commented Dec 28, 2009 at 5:14
  • 2
    Please post more code, at the very least the declarations & initializations of all participating elements. Ideally, post a small program that compiles & runs and demonstrates your problem. Simplifying your program in that way will often already point you to the solution. Commented Dec 28, 2009 at 5:15

5 Answers 5

90

As other people have mentioned, ArrayList has a constructor that takes a collection of items, and adds all of them. Here's the documentation:

http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html#ArrayList%28java.util.Collection%29

So you need to do:

ArrayList<MyNode> myNodeList = new ArrayList<MyNode>(this.getVertices());

However, in another comment you said that was giving you a compiler error. It looks like your class MyGraph is a generic class. And so getVertices() actually returns type V, not type myNode.

I think your code should look like this:

public V getNode(int nodeId){
        ArrayList<V> myNodeList = new ArrayList<V>(this.getVertices());
        return myNodeList(nodeId);
}

But, that said it's a very inefficient way to extract a node. What you might want to do is store the nodes in a binary tree, then when you get a request for the nth node, you do a binary search.

Sign up to request clarification or add additional context in comments.

2 Comments

Beware that it returns a shallow copy. Not a reference to the list actually.
@Chad Okere how is it possible to arraylist and not possible into a list?
26

Try this code

Convert ArrayList to Collection

ArrayList<User> usersArrayList = new ArrayList<User>();

Collection<User> userCollection = new HashSet<User>(usersArrayList);

Convert Collection to ArrayList

Collection<User> userCollection = new HashSet<User>(usersArrayList);

List<User> userList = new ArrayList<User>(userCollection);

Comments

7
public <E> List<E> collectionToList(Collection<E> collection)
{
    return (collection instanceof List) ? (List<E>) collection : new ArrayList<E>(collection);
}

Use the above method for converting the collection to list

Comments

6

The following code will fail:

List<String> will_fail = (List<String>)Collections.unmodifiableCollection(new ArrayList<String>());

This instead will work:

List<String> will_work = new ArrayList<String>(Collections.unmodifiableCollection(new ArrayList<String>()));

Comments

1

More information needed for a definitive answer, but this code

myNodeList = (ArrayList<MyNode>)this.getVertices();

will only work if this.getVertices() returns a (subtype of) List<MyNode>. If it is a different collection (like your Exception seems to indicate), you want to use

new ArrayList<MyNode>(this.getVertices())

This will work as long as a Collection type is returned by getVertices.

1 Comment

It returns a colletion type... but this one new ArrayList<MyNode>(this.getVertices()) does not work..

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.