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

Thread: Struggling with an Array of Arrays

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Struggling with an Array of Arrays

    I'm very new to Java and somewhat familiar with Processing.

    This is an easy one, but I searched the forum and couldn't find anything on the first few pages.

    Really struggling with creating a for loop to fill an array of arrays. Right now I only have 64 pairs of values, but I hope to create a version that has 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 values and it would be impossible to enter manually.

    This works, but I hate it:
    int[][] position = new int[][]{
    {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8},
    {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8},
    {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8},
    {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}, {4, 6}, {4, 7}, {4, 8},
    {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}, {5, 7}, {5, 8},
    {6, 1}, {6, 2}, {6, 3}, {6, 4}, {6, 5}, {6, 6}, {6, 7}, {6, 8},
    {7, 1}, {7, 2}, {7, 3}, {7, 4}, {7, 5}, {7, 6}, {7, 7}, {7, 8},
    {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 6}, {8, 7}, {8, 8},};

    Without a for loop the 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 version would start something like this:
    int[][] position = new int[][]{
    {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 2}, {1, 1, 1, 1, 1, 1, 1, 3}, {1, 1, 1, 1, 1, 1, 1, 4}, {1, 1, 1, 1, 1, 1, 1, 5}, {1, 1, 1, 1, 1, 1, 1, 6}, {1, 1, 1, 1, 1, 1, 1, 7}, {1, 1, 1, 1, 1, 1, 1, 8},
     
    {1, 1, 1, 1, 1, 1, 2, 1}, {1, 1, 1, 1, 1, 1, 2, 2}, {1, 1, 1, 1, 1, 1, 2, 3}, {1, 1, 1, 1, 1, 1, 2, 4}, {1, 1, 1, 1, 1, 1, 2, 5}, {1, 1, 1, 1, 1, 1, 2, 6}, {1, 1, 1, 1, 1, 1, 2, 7}, {1, 1, 1, 1, 1, 1, 2, 8},
     
    //etc. etc. until finally...
     
    {8, 8, 8, 8, 8, 8, 8, 1}, {8, 8, 8, 8, 8, 8, 8, 2}, {8, 8, 8, 8, 8, 8, 8, 3}, {8, 8, 8, 8, 8, 8, 8, 4}, {8, 8, 8, 8, 8, 8, 8, 5}, {8, 8, 8, 8, 8, 8, 8, 6}, {8, 8, 8, 8, 8, 8, 8, 7}, {8, 8, 8, 8, 8, 8, 8, 8},
     
    };

    thank you!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Struggling with an Array of Arrays

    Your answer lies in nested loops..

  3. The Following User Says Thank You to jps For This Useful Post:

    bhenry1790 (July 22nd, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Struggling with an Array of Arrays

    Quote Originally Posted by bhenry1790 View Post
    ...it would be impossible to enter manually....
    Of course it would.

    How did you come up with the sequence of arrays to put into your position array?

    Here's how I see it. I'll look at the arays as a set of digits, and I'll do it with three digits instead of eight

    The smallest value of each digit is 1. I'll call it minDigit. The largest value of each digit is 8. I'll call it maxDigit.
    Start the "counter" with a value of 111 (That is, create a array named "digits" and initialize it with the value minDigit in each position)

    How's how I would increment the counter:

    Increment the least significant digit (the LSD)
    The count is now 112. Increment again. Count is 113. Do it some more until the count gets to 118.

    Well---I have been having lots of fun just incrementing the LSD, but now I have a problem: When I increment the LSD, the digit value is greater than maxDigit. So what do I do? Easy, right? Set that digit to minDigit and increment the next digit up from there. So, the count goes from 118 to 121.

    Increment the LSD a bunch more times until I get to a count of 128. Now incrementing LSD causes me to have to reset to minDigit again and increment the next one up. So the count goes from 128 to 131, right?

    Do this a bunch more times, and the count gets to 188. Now when I increment the LSD, it's too big, so I reset it to minDigit and increment the next one up. But now that one is too big so I reset that one to minDigit and increment the next one up. So the count goes from 188 to 211.

    See how it goes?

    Here's a possible description of algorithm that can be converted directly to Java code:

    We have an array of bytes named digits that holds the count value. It was initialized to all 1s.

    Here's how to increment the count represented in the digits array:
    Increment:
        Make a loop that starts at the LSD (at the end of the array) and works back to the beginning of the array:
        for (int i = digits.length-1; i >= 0; i--)
        LOOP
            Increment digits[i]
            IF digits[i] IS GREATER THAN maxDigit
            THEN
                Set digits[i] EQUAL TO minDigit
                // That's all for this time through the loop.
                // As it continues through the loop, it will increment
                // the next digit up from here.
            ELSE
                 We are done for this incrementation: Break out of the loop
            END IF
        } // End of loop

    So, your loop to populate the position array goes like this:

         Declare the position array to your appropriate size.
         Make a loop that can go like this:
         for (int i = 0; i < position.length; i++)
        {
            Create an array of bytes, named c, equal to the current digits array.
     
            Copy the contents of c to position[i].
     
            Execute code to increment the count value in the digits array so
            that next time through the loop will store the next count value in
            the next position.
     
        } // End of loop

    (You don't necessarily need a separate array, c, in the middle of the loop, but having it broken out may make debugging easier, since you can print out the array and make sure the count is counting properly. Stuff like that.

    Note: I would use arrays of bytes instead of arrays of ints. I mean you are going to have 8 to the ninth power elements, and I definitely would not use larger array elements than necessary. Sometimes size matters.

    You can do things in-line in main(), or you can create a Counter class that takes care of initializing the digits array. The Counter class would need a method to return the current contents of the digits[] array and a method that increments the digits[] array according to our radix counting scheme above.

    Start with the way that seems "obvious" to you. Test it thoroughly with counters of three or four digits. Then, go for the biggie: Eight-digits and the corresponding array of 16,777,216 eight-byte arrays.

    Cheers!

    Z
    Last edited by Zaphod_b; July 25th, 2012 at 03:33 PM.

  5. The Following User Says Thank You to Zaphod_b For This Useful Post:

    bhenry1790 (July 22nd, 2012)

  6. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Struggling with an Array of Arrays

    Really struggling with creating a for loop to fill an array of arrays.
    Why do you need such a big array?

    What I'm thinking is that it's a lot ints. At 4 bytes per element Google says its 67Mb. Not enough to break the computer's bank, but if you don't intend changing more than a smallish number of elements perhaps some sort of sparse array would be a better data structure. A Map<Integer,Integer> would do, for instance.

    [Edit] Or if you don't intend changing any elements at all but, instead, intend iterating over the beast, write an Iterator that yields the right values in the right order.

    [Edit 2] Or is my maths off? If each of the 8^8 elements is going to hold a sequence of 8 four byte ints then we're at c500Mb for an array that - initially at least - holds next to zero information.
    Last edited by pbrockway2; July 21st, 2012 at 08:56 PM.

  7. The Following User Says Thank You to pbrockway2 For This Useful Post:

    bhenry1790 (July 22nd, 2012)

  8. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Struggling with an Array of Arrays

    Thanks for all the great reply's! Figured it would be a nested loop of some sort, arrays just seem very abstract to me... (not to mention ARRAYS of ARRAYS)

    You are awesome Z!! I think I can follow your advice... I have often done this visually drawings grids & patterns on screen (if x > edge of screen then y++).

    Why do you need such a big array?
    This is intended for an 8 motor mechanical sculpture. Each motor has 8 possible positions.

    A large 8^8 array will be filled with values, then shuffled. Then the 8 motor machine will go step by step throughout it's "life" (never repeating a previous position) according to the randomized path. At the end of the very long cycle and all 16.8 million positions have been realized it will refill the array, shuffle and start all over again.

    Looking into byte arrays now.

  9. #6
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Struggling with an Array of Arrays

    bhenry1790,
    what you did is syntactically wrong. Correct is, for example:
    int[][] products = { {0, 0, 0, 0, 0},
                        {0, 1, 2, 3, 4},
                        {0, 2, 4, 6, 8},
                        {0, 3, 6, 9, 12},
                        {0, 4, 8, 12, 16} };
    also: Nested {{{...}}} like you usually program with some nested loops

  10. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Struggling with an Array of Arrays

    Quote Originally Posted by Voodoo View Post
    bhenry1790,
    what you did is syntactically wrong
    No, it is not. It is valid Java and works just fine as written by the Original Poster.

    Quote Originally Posted by Voodoo View Post
    Correct is, for example:
    Your example is also correct, and many Java programmers prefer its elegance. I mean if you think it adds value to the discussion to present your stylistic preference, I have no problem with that, but I see no value in confusing the issue by pronouncing something "wrong" that is, in fact, not "wrong."

    Quote Originally Posted by Voodoo View Post
    Nested {{{...}}} like you usually program with some nested loops
    Maybe that's what you "usually" do.

    As for me: Sometimes yes; sometimes no. It "usually" depends on how the data are being generated (algorithmically or ad-hoc). For me, there is no "usual" way of initializing multi-dimensional arrays.


    Cheers!

    Z
    Last edited by Zaphod_b; July 23rd, 2012 at 09:46 AM.

Similar Threads

  1. [SOLVED] Filling a 2D array with other arrays.
    By mwebb in forum Object Oriented Programming
    Replies: 4
    Last Post: March 1st, 2012, 05:11 PM
  2. Arrays and array list
    By rob17 in forum Collections and Generics
    Replies: 2
    Last Post: February 19th, 2012, 06:10 PM
  3. sum of arrays and printing even and odd numbers in the array
    By senecawolf in forum Collections and Generics
    Replies: 3
    Last Post: November 8th, 2011, 03:07 PM
  4. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: December 1st, 2008, 09:02 AM
  5. Details about 'CopyTo' of Arrays in Java
    By Fendaril in forum Collections and Generics
    Replies: 18
    Last Post: November 13th, 2008, 08:31 AM