Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

arraylists and arrays

807600Nov 5 2007 — edited Nov 6 2007
i'm trying to put the numbers in an arraylist into an int[] array. could anyone tell me how this could be done? i tried the below but i get an error pointing to return divisors.toArray();
import java.util.ArrayList;
import java.util.List;

class FactorsDemo {

    public static void main(String[] args) {
	//for(int i = 10; i < 30; i+=5) {
	    int[] divisors = getDivisors(10);
	//}
    }

    static int[] getDivisors(int n) {
	List<Integer> divisors = new ArrayList<Integer>();
	for(int d = 1; d <= n/2; d++) {
	    if(n%d == 0) divisors.add(new Integer(d));
	}
	return divisors.toArray();
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 4 2007
Added on Nov 5 2007
6 comments
161 views