title: Is there any way for Array Elements to be created through a for-loop?
body: <p>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?</p>
  
  <p>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. </p>
  
  <pre><code>public static int[] allDivisors(int number) {
      int[] divisors;
      int counter= 0;
  
      for (int i = 0; i &lt;= number; i++) {
          if(number % i == 0) {
             divisors[counter]= i;
             counter++;
          }
      }
      return divisors;
  }
  </code></pre>
  
  <p>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!</p>