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?
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:
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:
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.