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!