简体   繁体   中英

Is there any way for Array Elements to be created through a for-loop?

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!

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 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM