1

The method signature looks like this:

            public void addThemAll(Collection<? extends T> c)

Which essentially just adds every element of the collection to my LinkedList. But I keep trying to feed this method an Array or a Linked List and I always get an error. For example:

            double[] myarray = new double[]{3.4, 4.5, 8.6};
            mylist.addThemAll(myarray);

I'm sure this is something straightforward, but I can't find an example online that just passes an array/linked list into a method like this.

5
  • How are you declaring your linked list? What is the type you parametrize for T?
    – nanofarad
    Sep 18, 2013 at 23:59
  • 3
    An array is not a collection. Sep 19, 2013 at 0:00
  • mylist is a double as well Sep 19, 2013 at 0:02
  • How have you defined T in your class?
    – Bohemian
    Sep 19, 2013 at 0:03
  • (There is Collections.addAll (Collections with an s) which will the elements of an array to a collection. But lowercase double is not a reference, so you can't have a List of it (needs to be boxed to a java.lang.Double).) Sep 19, 2013 at 0:18

3 Answers 3

4

Your code has two problems:

  1. An array is not a collection. It does not extend Collection. Therefore, you can't pass it into a method whose signature specifies a collection parameter.
  2. You have not defined <T> (or, at least, you have not shown us where you are defining <T>). You can either define <T> in your class, or in your method signature.

To define it in your class, do it like this:

public class MyClass<T> {
    // contents
}

To define <T> in your method, do it like this:

public <T> void addThemAll(Collection<? extends T> c) {
    // method logic
}
0

For what you are doing, this would work:

List<Double> myArray = Arrays.asList(3.4, 4.5, 8.6);
mylist.addThemAll(myarray);

The reason being is that you are passing in a list (which is a collection). Currently you are passing in an Array, which is not a collection.

0
0

To pass in the array to collection:

Double[] myarray = new Double[]{3.4, 4.5, 8.6};
        mylist.addThemAll(Arrays.asList(myarray));

if you don't want it as list but want it as LinkedList or etc

LinkedList<Double> linkedlist = new LinkedList(Arrays.asList(myarray));
mylist.addThemAll(linkedlist);

if you want to use set or treeset

TreeSet <Double> treeset = new TreeSet(linkedlist);

Difference between set and list is that set does not have duplicate and not ordered, and list is ordered but contains duplicates.

After you pass in to your method:

public void addThemAll(Collection<? extends T> c)
if(c instanceof LinkedList){
LinkedList a = (LinkedList) c //you can invoke methods from LinkedList
....
}
3
  • Passing an array of doubles to Arrays.asList() will result in an List of size 1, with the entire array as the first element.
    – dnault
    Sep 19, 2013 at 2:06
  • Arrays.asList(array) will return list view of the array. It means list will have each element of the array as is in the same order of index. Thus list will not have size of 1 but size of 3 in this case. The type wrapper class will have no effect since the list is generic.
    – JungJoo
    Sep 19, 2013 at 5:21
  • @dnault you are actually right, I used Double in my test class. Using primitive type array, it does cause some error.
    – JungJoo
    Sep 19, 2013 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.