-3

So, I wanted to write a method that would create an array containing all divisors of a certain number. So I created an empty array (int[] divisors) that would later get it's numbers through a for-loop but it says that the array hasn't been initialized yet. What should I do?

The output of the method would later be used to find the greatest common divisor of two numbers, so it's important that the numbers in the array are ordered from smallest to biggest. I know there's a much easier solution to do this, but I want do it using arrays, because I want to focus on learning those at the moment.

public static int[] allDivisors(int number) {
    int[] divisors;
    int counter= 0;

    for (int i = 0; i <= number; i++) {
        if(number % i == 0) {
           divisors[counter]= i;
           counter++;
        }
    }
    return divisors;
}

I couldn't find any proper solutions to my problem online so I hope someone we'll be able to help me out here. All I need is a way to add specific elements to an array which I can't define beforehand (in that case, all divisors of a certain number.) Thanks in advance for all your answers!

  • Possible duplicate of [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – Marv Dec 26 '18 at 15:07
  • You need to call new int[size], where size is the length of the array you need. – duffymo Dec 26 '18 at 15:08
  • Hey duffymo, thanks for your answer! Thing is, I can't know how many divisors the number has beforehand, so I can't define a certain length. – Electrifying Onyx Dec 26 '18 at 15:19
  • Use a list instead of an array and then convert to arrays. – Nikhil Dec 26 '18 at 16:40

3 Answers3

2

Array is not the proper data structure for this case because you need to initialize it with a size (integer number) prior of its use.
But you don't know how many items you will store in the array, right?
So you must use an ArrayList like this:

public static ArrayList<Integer> allDivisors(int number) {
    ArrayList<Integer> divisors = new ArrayList<>();

    for (int i = 1; i <= number; i++) {
        if(number % i == 0) {
            divisors.add(i);
        }
    }
    return divisors;
}

I also changed in the loop:

int i = 0

to

int i = 1

to avoid division by 0 later in:

number % i

Also you don't need counter.
You can call this method in your code:

int number = 30;
ArrayList<Integer> list = allDivisors(number);

System.out.println("The divisors of " + number + " are:");
for (int i = 0; i < list.size(); i++) {
    System.out.print(list.get(i) + " ");
}

and it will print:

The divisors of 30 are:
1 2 3 5 6 10 15 30 
forpas
  • 117,400
  • 9
  • 23
  • 54
  • Hey, thanks for you answer! it seems like this could really work, the only problem I have now is that my Java doesn't know what an ArrayList is. Could it be that my Java version doesn't supprt this data type yet? I'm using JDK 8 for school purposes so it might be that I'll need a more recent version for that, right? – Electrifying Onyx Dec 26 '18 at 15:28
  • There is no case that *Java does not know what an ArrayList is*. What ide are you using? – forpas Dec 26 '18 at 15:32
  • @ElectrifyingOnyx Did you `import java.util.ArrayList;`? – GBlodgett Dec 26 '18 at 15:34
  • Well, it says "Can't resolve symbol 'ArrayList'" I should've been more specific, sorry. I'm using IntelliJ IDEA. – Electrifying Onyx Dec 26 '18 at 15:37
  • If you are using intelij, place the cursor at ArrayList and press Alt+Enter. – forpas Dec 26 '18 at 15:37
  • Or add this: `import java.util.ArrayList;` at the imports section of your class before the class declaration. – forpas Dec 26 '18 at 15:38
  • @ElectrifyingOnyx if this answer helped you you might consider accepting it. – forpas Dec 26 '18 at 15:56
0

If you strictly want to do this using arrays, try below changes to your program.

public static int[] allDivisors(int number) {
  int[] divisors = new int[number]; // Have to initialize the array
  int counter= 0;

  for (int i = 1; i <= number; i++) { // i should start from 1; not from zero. Otherwise you get ArithmeticException (divide by zero)
    if(number % i == 0) {
      divisors[counter]= i;
      counter++;
    }
  }


  int[] trimmedDivisors = new int[counter];
  System.arraycopy(divisors, 0, trimmedDivisors, 0, counter);

  return trimmedDivisors;
}
Prasad Karunagoda
  • 1,878
  • 2
  • 11
  • 14
0

In java before you use array you must be initializate it.

public static int[] allDivisors(int number) {
        int[] divisors = new int[number];
        int counter= 0;

        for (int i = 0; i <= number; i++) {
            if(number % i == 0) {
                divisors[counter]= i;
                counter++;
            }
        }
        Arrays.sort(divisors);
        return divisors;
    }
TongChen
  • 1,240
  • 1
  • 8
  • 17