4

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!

5
  • 1
    possible duplicate of How to convert an ArrayList containing Integers to primitive int array?
    – Alexis C.
    Commented Oct 18, 2013 at 8:28
  • 1
    Java Collections can only hold objects. int is a primitive data type and cannot be held in an ArrayList for example. You need to use Integer instead. stackoverflow.com/questions/960431/…
    – muthu
    Commented Oct 18, 2013 at 8:30
  • Do you use an IDE, like Netbeans, IntelliJ or Eclipse?
    – Reporter
    Commented Oct 18, 2013 at 8:32
  • I do not like overcomplicated examples. Can you prove that you cannot demonstrate what you want to do in one line? Why the cycle is important?
    – Val
    Commented Oct 18, 2013 at 8:38
  • Yeah, sorry for posting a duplicate post. It was 2:00 am, and I was tired of trying to find relevant posts. And sorry Val. Again, it was 2:00 am. Commented Oct 18, 2013 at 20:39

3 Answers 3

8

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 ints 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, ...)

2
  • Thank you so much! I was completely unaware that there was a difference between int and Integer. Commented Oct 18, 2013 at 20:33
  • Welcome to StackOverflow! Glad that helped. Remember to upvote helpful answers and accept the one you think better answered your question (see How does accepting an answer work?). It will help other people visiting this question in the future. Commented Oct 19, 2013 at 0:41
-1
int[] primeArray = primeList.toArray(new int[primeList.size()]);

but I'm not really confident to be able to do this with int than with Integer

0
-2

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]);
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.