Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using the Arrays.asList().contains() method in my code, as shown in the top answer: In Java, 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.

share|improve this question
    
Actually, I am thinking it may be because Arrays.asList returns a list, not an array list. Would that be it? –  user16647 Jun 1 '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 1 '12 at 23:26

3 Answers 3

up vote 5 down vote accepted

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.

share|improve this answer
    
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. –  owlstead Jun 1 '12 at 23:30
    
Thanks!........ –  user16647 Jun 1 '12 at 23:38
4  
This looks to of been missed from the discussion, but Arrays.asList(primes) returns a typed List<int[]>. The generics is treating the int[] as the Type parameter where asList() would only add a single object, the primes object. So it actually has nothing to do with it not being able to autobox the array into a List<Integer>, it's that int is not a valid Generic Type period. –  Andrew Finnell Jun 1 '12 at 23:46
    
@AndrewFinnell thanks I'll update my answer –  Jeshurun Jun 1 '12 at 23:49
    
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 2 '12 at 3:08

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.

share|improve this answer

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;
}
share|improve this answer
    
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 2 '12 at 3:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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