Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Assgining values to array indexes

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Assgining values to array indexes

    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:
    7
    7

    i expect it to be ..
    1
    7

    help here pls


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Assgining values to array indexes

    Are we supposed to guess what you're trying to do?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (December 28th, 2009)

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Assgining values to array indexes

    would you mind to check this world?

       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

Similar Threads

  1. Replies: 1
    Last Post: December 22nd, 2011, 09:55 AM
  2. Get <SELECT> tag values on other jsp page
    By nehakuls in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 18th, 2009, 02:29 AM
  3. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM
  4. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM
  5. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM