# Assgining values to array indexes

• December 28th, 2009, 01:26 AM
chronoz13
Assgining values to array indexes
Code :

```public static void main(String[] args) {   int[] divisors = new int[0];   int number = 7, divisibilityCount = 0;   for (int x = 1; x <= number; x++) {   if (number % x == 0) {   divisibilityCount++; divisors = new int[divisibilityCount];   for (int i = 0; i <= divisors.length - 1; i++) {   if (divisors[i] == 0) {   divisors[i] = x; } else {   divisors[i + 1] = x; } } } }   for (int x = 0; x <= divisors.length - 1; x++) {   System.out.println(divisors[x]); } }```

OUTPUT:
Code :

```7 7```

i expect it to be ..
Code :

```1 7```

help here pls
• December 28th, 2009, 11:22 AM
dlorde
Re: Assgining values to array indexes
Are we supposed to guess what you're trying to do?
• December 28th, 2009, 05:33 PM
helloworld922
Re: Assgining values to array indexes
It looks like a number factor generator. The reason it's giving you that is because you're creating a new array, but you're not copying over all the old factors, but instead populating the new array with just the new factor. I'm guessing that if you tried this with 12, the array would be all 12's instead of the expected {1,2,3,4,6,12}.

FYI, it's much more efficient to not allocate a new array each time you find a factor, but instead double it's size each time the size limit has been reached. Then, at the end trim the array down to size (aka. create the correct size array). A second alternative is to count the number of factors first, then only allocate one array of factors that is the right size to begin with, then re-calculate the factors and put them in.
• December 28th, 2009, 10:09 PM
chronoz13
Re: Assgining values to array indexes
would you mind to check this world?

Code :

``` public static void main(String[] args) {   int[] divisors;   int number = 3, divisibilityCount = 0;   /** * before we assign the values in an array, lets count * first how many numbers should WE ASSIGN in an array, * it wil server as the length of the new array */ for (int x = 1; x <= number; x++) {   if ((number % x) == 0) {   divisibilityCount++; } }   // instantiate the new array divisors = new int [divisibilityCount];   int count = 0;   // do the math again, but now we will assign the values in the new array for (int x = 1; x <= number; x++) {   if ((number % x) == 0) {   divisors[count] = x; count++; } }   System.out.print("The Number: " + number + " Is A Prime Because It Is" + " Only Divisible By: ");   // display the values for (int x = 0; x <= divisors.length - 1; x++) {   System.out.print(divisors[x]);   if (x < divisors.length - 1) {   System.out.print(", "); } }   System.out.print("\n"); }```

i have two loops, one that will count all the needed values,, then that count will be the new length of
the new allocation of my array.

then on my second loop the values will be assigned in the new allocated array..

is this an efficient way to do my program...?

anyway this is what i want in my program regarding with the array..

1.) the array will not be instantiated if the values are not yet counted
2.) the array length will depend on the number of the values that will be generated..

ofcourse we know that prime numbers are ony divisible by two.. so why didnt just make an array with length of two? i'm just following the case that has given to me,.. and i want to learn this logic...
and i can use this algorithm for some future purposes