简体   繁体   中英

How do I convert an ArrayList to an Array then return it in Java?

I'm trying to create a method that creates a list of prime factors of a given number, then returns them in an array. Everything seems to be working fine except for the conversion of the ArrayList to an Array. Also, I'm not sure if I'm returning the array correctly.

Here's my code...

static int[] listOfPrimes(int num) {
    ArrayList primeList = new ArrayList();
    int count = 2;
    int factNum = 0;

    // Lists all primes factors.
    while(count*count<num) {
        if(num%count==0) {
            num /= count;
            primeList.add(count);
            factNum++;
        } else {
            if(count==2) count++;
            else count += 2;
    }
}
int[] primeArray = new int[primeList.size()];
primeList.toArray(primeArray);
return primeArray;

It returns this error message when I compile...

D:\JAVA>javac DivisorNumber.java
DivisorNumber.java:29: error: no suitable method found for toArray(int[])
            primeList.toArray(primeArray);
                     ^
method ArrayList.toArray(Object[]) is not applicable
  (actual argument int[] cannot be converted to Object[] by method invocatio
n conversion)
method ArrayList.toArray() is not applicable
  (actual and formal argument lists differ in length)
Note: DivisorNumber.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Additionally, I'm not sure how to receive the returned array, so I need some help on that as well. Thanks!

If you want to use the generified toArray() method, you'll need to use the Integer wrapper class instead of the primitive type int .

Integer[] primeArray = new Integer[primeList.size()];
primeList.toArray(primeArray);

The error the compiler is giving is stating that the method you want to call ( List#toArray(T[]) ) doesn't apply to an argument of type int[] , just because an int is not an Object (it is a primitive type). An Integer is an Object however, wrapping an int (and this is one of the main reasons the Integer class exists).

Of course you could also iterate through the List manually and add the Integer elements in it as int s in an array.

There's a related question here on SO: How to convert List to int[] in Java? with lots of other suggestions (Apache commons, guava, ...)

int[] primeArray = primeList.toArray(new int[primeList.size()]);

但我对使用int不是使用Integer能够做到这一点并不是很有信心

change int[] array to Integer[]

static Integer[] listOfPrimes(int num) {
    List<Integer> primeList = new ArrayList<Integer>();
    int count = 2;
    int factNum = 0;

    // Lists all primes factors.
    while (count * count < num) {
        if (num % count == 0) {
            num /= count;
            primeList.add(count);
            factNum++;
        } else {
            if (count == 2)
                count++;
            else
                count += 2;
        }
    }

    return primeList.toArray(new Integer[0]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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