1

I am am trying to use the LinkedList class to load an array of anything. 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(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[]
2
  • I went with an ArrayList but I am itching to know why this is not working??? Commented Dec 11, 2011 at 9:55
  • See updated answer for reason. Commented Dec 25, 2011 at 7:08

3 Answers 3

2

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.

If you don't need an array, use collection classes from start only(like ArrayList or 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)-

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

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

3 Comments

I understand what you are saying and thank you for your help. Its just that my team mates decided to use Arrays for the project(I didnt agree). I will be removing and adding objects because the list will be increasing and decreasing randomly. But I need to get their array in to my list.
@Henry then new ArrayList<Element>(Arrays.asList(array)) is a way to go :)
Thank you Premraj, I will go with an ArrayList. It does make sense and it will do what I need.
1

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.

Comments

0

You can use the java.util.Arrays class:

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

5 Comments

...or the OP could've used ArrayList<Object> instead of an array.
This only adds the instance of the array to the list not the content of the array.
I cant do anything on the array side. This project is being done individually by people. I want a list because I dont have to shift or worry about dead spots(nulls).
@Henry: Arrays.asList transforms the array into a list backed by this array. But you won't be able to add or remove alements from the returned list.
Henry, you should not do this if you want to modify resulting List! Arrays.asList() returns not a regular List implementation(ArrayList or LinkedList from java.util package), but unmodifiable static ArrayList class from java.util.Arrays. You should use new ArrayList<MyType>(Arrays.asList(array)) in this situation.

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.