简体   繁体   English

使用LinkedList Java类

[英]Using LinkedList Java Class

I am am trying to use the LinkedList class to load an array of anything. 我正在尝试使用LinkedList类来加载任何数组。 I was looking at the documentation of addAll method and it seem to me that I could load an array of stuff with very little effort. 我正在查看addAll方法的文档,在我看来,我可以用很少的努力加载一系列的东西。

 addAll(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list, starting at the specified position. 

But I am not so sure on how to structure it. 但是我不确定如何组织它。

Let's just say I have an array of Objects and I am trying to load the objects into a link list. 我只想说我有一个对象数组,我正在尝试将对象加载到链接列表中。
How would I write this? 我怎么写这个?

NOTE: I am trying to load it to a list because I will be deleting and adding at multiple times. 注意:我试图将其加载到列表中,因为我将多次删除和添加。

I used below code but I get an error: 我使用下面的代码,但出现错误:

    int[] a = new int[5];
    for(int index = 0; index<5; index++){
        a[index] = index;

    }

    List<Integer> list = new LinkedList<Integer>();
    list.addAll(Arrays.asList(a)); //error Here won't take a[]

If you need specifically LinkedList or mutable collection class (you mentioned add/delete operations) then Arrays.asList(T...) won't help as it returns a fixed-size list backed by the specified array and hence not mutable and Arrays.asList(T...).add(T) call will always fail. 如果您需要特定的LinkedList或可变集合类(您提到添加/删除操作),那么Arrays.asList(T...)将无法帮助,因为它返回由指定数组支持的固定大小列表,因此不可变和Arrays.asList(T...).add(T)调用总是会失败。

If you don't need an array, use collection classes from start only(like ArrayList or LinkedList). 如果不需要数组,请仅从开始使用收集类(例如ArrayList或LinkedList)。 In case if it's not possible this collection tutorial will give you fair idea about usage and other aspects. 如果不可能的话,本收集教程将为您提供有关用法和其他方面的合理建议。

if you wanto to create an ArrayList (or LinkedList) you can use (and it will copy all the elements of an array to List)- 如果你想创建一个可以使用的ArrayList(或LinkedList)(它会将数组的所有元素复制到List) -

new ArrayList<Element>(Arrays.asList(array))


EDIT - 编辑 -

If the array itself is a primitive array (which you shown in example) then above approach will not work either because Arrays.asList(int[]) will actually return single element List<int[]> and that's why you're getting error in your example. 如果数组本身是一个原始数组(你在例子中显示),那么上面的方法将无法工作,因为Arrays.asList(int[])实际上将返回单个元素List<int[]> ,这就是你得到错误的原因在您的示例中。
If you find yourself working heavily with collection of primitives then I would suggest to look at Trove or may be Guava which provides awesome utilities pertaining to collections and other day to day needs(it has Ints.asList() method which satisfy your requirement, see doc ). 如果你发现自己正在大量使用基元的集合,那么我建议你看一下Trove,或者可能是Guava ,它提供了与集合和其他日常需求有关的很棒的实用程序(它有Ints.asList()方法,满足你的要求,见doc )。

Start by transforming the array into a list, and then add all the elements of this list to your linked list: 首先将数组转换为列表,然后将此列表的所有元素添加到链接列表中:

List<Foo> list = new LinkedList<Foo>();
list.addAll(Arrays.asList(myArray());

Or just iterate through the array yourself, and add every element: 或者您自己遍历数组,并添加每个元素:

List<Foo> list = new LinkedList<Foo>();
for (Foo foo : myArray) {
    list.add(foo);
}

which requires just one more line of code, is straightforward, and more efficient. 它只需要多一行代码,就更加简单,高效。

Note that ArrayList is more efficient than LinkedList in most situations. 请注意,在大多数情况下,ArrayList比LinkedList更有效。

您可以使用java.util.Arrays类:

List<MyType> list = Arrays.asList(array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM