Java - 如何将类型集合转换为 ArrayList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1968068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:54:51  来源:igfitidea点击:

Java - How to convert type collection into ArrayList?

javacollections

提问by user236691

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

线程“main”中的异常 java.lang.ClassCastException: java.util.Collections$UnmodifiableCollection 不能转换为 java.util.ArrayList...

Can anyone help?

任何人都可以帮忙吗?

采纳答案by Chad Okere

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

正如其他人所提到的,ArrayList 有一个构造函数,它接受一组项目,然后添加所有项目。这是文档:

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

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 genericclass. And so getVertices() actually returns type V, not type myNode.

但是,在另一条评论中,您说这会给您带来编译器错误。看起来您的类 MyGraph 是一个通用类。因此 getVertices() 实际上返回类型 V,而不是类型 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.

但是,这表示这是一种非常低效的提取节点的方式。您可能想要做的是将节点存储在二叉树中,然后当您收到第 n 个节点的请求时,您会进行二分查找。

回答by sleske

More information needed for a definitive answer, but this code

确定答案需要更多信息,但此代码

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

will onlywork 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

只有当工作this.getVertices()回报(亚型)List<MyNode>。如果它是一个不同的集合(就像你的 Exception 似乎表明的那样),你想使用

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

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

只要 getVertices 返回 Collection 类型,这就会起作用。

回答by Dwivedi Ji

Try this code

试试这个代码

Convert ArrayList to Collection

将 ArrayList 转换为集合

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

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

Convert Collection to ArrayList

将集合转换为 ArrayList

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

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

回答by Sandeep Bhardwaj

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

使用上述方法将集合转换为列表

回答by Mircea Stanciu

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>()));