8

I'm using the Arrays.asList().contains() method in my code, as shown in the top answer: How can I test if an array contains a certain value?, so I am going to use Arrays.asList() in the code.

However, the compiler rejects this following code. Is it because of using primitives for my primes array, rather than a reference type? I don't think so, due to autoboxing, but I just wanted to check.

import java.math.*;
import java.util.ArrayList;
import java.util.Arrays;

public class .... {
    public static void main(String[] args) {
        int[] primes = formPrimes(15);
        ArrayList<Integer> primes1 = new ArrayList<Integer>(Arrays.asList(primes));
        // Rest of code...
    }

    public static int[] formPrimes(int n) {
        // Code that returns an array of integers
    }
}

I get one error, a cannot find symbol error.

symbol : constructor ArrayList(java.util.List)

location: class java.util.ArrayList ArrayList primes1 = new ArrayList(Arrays.asList(primes));

Basically, I've got a function returning an array of integers, and I want to convert it into an array list, and I'm running into trouble with using the ArrayList constructor.

Community
  • 1
  • 1
user16647
  • 165
  • 2
  • 3
  • 10
  • Actually, I am thinking it may be because Arrays.asList returns a list, not an array list. Would that be it? – user16647 Jun 01 '12 at 23:20
  • No, that wouldn't be it--both an ArrayList and a List are a Collection, which is what the ArrayList ctor expects. It's because you're mixing types. – Dave Newton Jun 01 '12 at 23:26

3 Answers3

10

Yes. Autoboxing does not apply to arrays, only to primitives.

The error I get in eclipse is The constructor ArrayList<Integer>(List<int[]>) is undefined

Thats because the constructor in ArrayList is defined as public ArrayList(Collection<? extends E> c). As you can see, it only accepts a subtype of Collection, which int is not.

Just change your code to:

public class .... {
    public static void main(String[] args) {
        Integer[] primes = formPrimes(15);
        ArrayList<Integer> primes1 = new ArrayList<Integer>(Arrays.asList(primes));
        // Rest of code...
    }

    public static Integer[] formPrimes(int n) {
        // Code that returns an array of integers
    }
}

and all should be well, assuming you return an Integer array from fromPrimes.

Update From Andrew's comments, and after peeking into the source of Arrays.asList:

public static <T> List<T> asList(T... a) {
    return new ArrayList<T>(a);
}

So what is really happening here is that Arrays.asList(new int[] {}) would actually return a List<int[]>, unlike Arrays.asList(new Integer[] {}) which would return aList<Integer>. Obviously the ArrayList constructor will not accept a List<int[]>, and hence the compiler complains.

Jeshurun
  • 22,940
  • 6
  • 79
  • 92
  • I think the problem lies more in the fact that you don't want to have a list of arrays, rather a list of items in the array. – Maarten Bodewes Jun 01 '12 at 23:30
  • Andrew, this also explains why a similar code of mine using Arrays.asList() worked for string arrays, since strings are clearly a valid generic type. Thanks for the explanation! – user16647 Jun 02 '12 at 03:08
1

primes is an array of the primitive int type; that type does not derive from Object and can therefore be automatically placed into a List (which, like all Collections, can only hold Objects). @Justin is right; you need to manually add the items from your array to the list.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
0

If you're doing this to get access to the contains method you may as well write your own.

public boolean contains(int[] array, int item) {
  for (int element: array)
    if (element == item)
      return true;

  return false;
}
Ricky Clarkson
  • 2,909
  • 1
  • 19
  • 21
  • Yeah, that's another alternative. I was trying to get access to the contains method, and I guess I didn't really think of this. – user16647 Jun 02 '12 at 03:05