Results 1 to 10 of 10
  1. #1
    naclh2onaz is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Stuck on Two Dimensional Arrays

    I go to school online and am having a tough time with my Java course. Currently, im having trouble grasping 2 dimensional arrays as the book is poor at explaining why and just uses code without explaining it.
    I have a certain problem I need help understanding:

    String[][] words = {
    {"Red", "Blue", "Green"},
    {"Orange", "Purple". "Yellow"}
    };

    for (int I= words.length -1; I >= 0; I--){
    for (int j=0; j < words[I].length; j++) {
    System.out.print(words[I][j]);

    I ran the code and got OrangePurpleYellowRedBlueGreen but I'm not sure how that is
    TIA

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Stuck on Two Dimensional Arrays

    First, there are really no such things a multidimensional arrays in Java. What a "2D" array really is is a "!D" array, each element of which holds reference to another "1D" array. So you have an array of arrays.

    So you could do this:

    int[][] int a = new int[5][6];

    import java.util.Arrays;

    Java Code:
    public class MultiArrayEx {
    
       public static void main(String[] args) {
          int[][] a = new int[5][6];
    
          int count = 1;
          for (int j = 0; j < a.length; j++) {
              int[] temp = a[j];
              for (int k = 0; k < temp.length; k++) {
                   temp[k] = count++;
              }
          }
    
          for (int j = 0; j < a.length; j++) {
            System.out.println(Arrays.toString(a[j]));
          }
    
       }
    
    }
    You don't have to do it this way. Your way is fine. I just fine the above is cleaner and also helps to explain the process.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    naclh2onaz is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: Stuck on Two Dimensional Arrays

    I didn't write the code, its in a pre-test and Im having trouble understanding how I got the result

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Stuck on Two Dimensional Arrays

    Well, when you print the values you are printing them side by side on the same line with no spaces. Try it this way and see if it makes more
    sense.

    Java Code:
    String[][] words = { 
     {"Red", "Blue", "Green"}, 
     {"Orange", "Purple". "Yellow"} 
     }; 
     
     for (int I= words.length -1; I >= 0; I--){ 
        for (int j=0; j < words[I].length; j++) { 
          System.out.print(words[I][j] + " " );  // added a space between values
        }
        System.out.println();  // print a new line
      }

    Then have a look at Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics). If you are still confused, ask a more specific question and we can try to help.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Stuck on Two Dimensional Arrays

    Might be worth it to write out the creation of the array in the more verbose way and really treat it as an '1D array of 1D array'.

    Java Code:
    // create an array which will hold an array in each index
    String[][] colors = new String[2][];
    
    // now assign an array to each index
    colors[0] = new String[] { "Red", "Green", "Blue"};
    colors[1] = new String[] { "Yellow", "Orangle", "Purple"};
    
    // and you can also get each individual array array back again like this
    String[] colorline1 = colors[0];
    String[] colorline2 = colors[1];
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    naclh2onaz is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: Stuck on Two Dimensional Arrays

    The part im having trouble with is:
    for (int I= words.length -1; I >= 0; I--){
    for (int j=0; j < words[I].length; j++) {
    System.out.print(words[I][j]);

    what does words.length -1;I >=0; I-- mean in English?
    and j=0; j< words[I].length; j++?

    Ive used the school book, watched videos on newboston and bought an additional book but they don't explain what these mean

  7. #7
    naclh2onaz is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: Stuck on Two Dimensional Arrays

    Ok, I get that the array is named words and words.length is the number of items in the array. So there are 3 items in the I array, Im not sure what the -1 is doing in the first for statement, and I know that I-- is decrementing, but how can I decrement the array?

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Stuck on Two Dimensional Arrays

    What do you think "decrementing the array" will do? It makes no sense. You decrement numbers, not an array.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    169
    Rep Power
    1

    Default Re: Stuck on Two Dimensional Arrays

    I'm having a hard time understanding what you mean, but I'll try my best.

    Multidimensional arrays are arrays within arrays, so like String[][][] abc; is a three dimensional array, int[][] is a two-dimensional. Two dimensional arrays have arrays inside of arrays, whereas three dimensional arrays have arrays within arrays within arrays, and it goes on like that. I think the idea for DIMENSIONAL comes from how you can use 2D arrays as a co-ordinate storage. You can do something like int[][] coords, and then coords[x][y].

    Single D arrays can have values {value, value, value}, Two D arrays can have values as {{value, value}, {value, value}, {value, value, value}} etc. And Three Have {{{},{},{},{}}}. And it goes on like this. All values such as integers, strings and others are located in the furthest brackets, so a int[][][], all numbers would be typed up in only the further brackets so a 3D array {can't go here,{can't go here,{CAN GO HERE!}}} The can't go here's are basically nothing, and if you put something there in a 3D array you'll get an error, you can only put values inside the third. And you can have multiples.

    Your code first finds the length of the entire array which, has two arrays in it, making the number two. So array {{value, value, value},{value,value,value}}. Now the code DOESN'T care what's inside the red shown arrays, it only looks for HOW MANY there are. So your first loop cycles through the array checking for how many values are inside, NOT checking what's inside THEM.

    Then when you have an array {{value, value, value}, {value, value, value}}, and you do array[0] you GO INSIDE the first VALUE which is the first array containing value, value, value. When you do Array[number].length you CHECK the length of the array that is INSIDE the main array.

    Your second loop loops through all the values inside the selected ARRAY in the main array. Printing out, red, blue, green or whatever.

    Some Semi-Visual Basics:

    Java Code:
    int[][] array = {{5, 5, 6, 7}, {2, 2, 3}, {3, 3, 1, 1, 1, 1}};
    int a1 = array.length; //Is going to be 3, because there are three values in the entire array.
    int a2 = array[1].length; //Is going to be 3, because array[1] is the {2, 2, 3} and how many values are in that?
    //Well yes THREE!
    
    int[][][] arr = {{{10, 10}, {10, 10, 10, 10}}, {{5, 5, 5, 5, 5, 5, 5, 5}, {5, 5}}, {{6, 6, 7, 8, 9}}};
    int a3 = arr.length; //Is going to be 3, because there are 3 arrays inside the prime array.
    int a4 = arr[0].length; //Is going to be 2, because there are 2 arrays in the first array in the prime array.
    int a5 = arr[1][0].length; //Is going to be 8, because the second array in the prime array, in which the first array
    //has 8 number fives, making it 8!!!
    
    int a6 = arr[2][0][4]; //Is going to be 9, because the last array's first array's last value which has the index of 4
    //, is going to be 9!!!
    
    //Now for the MULTIDIMENSIONS and X and Y
    
    //Let's say you have a game with a map 5 tiles height and 5 tiles width, in total having 25 tiles.
    String[][] pos = new int[5][5]
    //Now let's say your character is on x: 3, y: 2 from the top-left corner of your screen. Since arrays start from 0
    //, that's 2, 1
    System.out.print(pos[x][y]); //this will print out whatever tile is there.
    //Now how the whole map will look like
    //1   6   11   16   21
    //2   7   12   17   22
    //3   8   13   18   23
    //4   9   14   19   24
    //5   10  15   20   25
    
    //The array's places are {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20},
    //{21, 22, 23, 24, 25}}
    //It has 5 arrays with 5 values inside each, new int[5][5]. So X since it's the first, it says ok what's
    //in the first X value, there are 1 2 3 4 5
    //Then there are in the second X Ys of 6, 7, 8, 9, 10.
    This might be very confusing, or very helpful. Lol
    Augustas
    Last edited by augustas656; 04-19-2014 at 01:48 AM.

  10. #10
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Stuck on Two Dimensional Arrays

    Hmm ... Maybe the confusion of the op is now just the for loop. So let us examine it in detail:
    for (int I= words.length -1; I >= 0; I--)
    The for loop has 3 areas which are separated by ;
    1) the initialisation part: int l=words.length-1
    2) the check that should be done before any iteration: l>=0
    3) the modification after each iteration: l--

    So first we get a new local variable and initialise it to the size of the array minus 1.
    When we have 3 items in an array, the size is 3. But that means that the elements are 0, 1, and 2. So with minus 1 we get the highest element.
    And the check is looking if we are still inside the array. The first element is number 0 so we check if l is higher or equal 0.
    And after each iteration we decrease the local variable. So let us see what is going on with 3 elements in an array.
    New local variable l, set to 3-1=2
    Check: 2>=0? Yes
    => execution of the stuff inside the loop
    Decrease l: l is now 1
    Check: 1>=0? Yes
    => execution of loop content
    Decrease l: l is now 0
    Check: 0>=0? Yes
    => execution of loop content
    Decrease l: l is now -1
    Check: -1>0? No
    => loop is no longer executed
    Local variable l goes "out of scope"

    Konrad

Similar Threads

  1. Two dimensional arrays
    By mackenzie1235 in forum New To Java
    Replies: 12
    Last Post: 12-16-2013, 05:40 PM
  2. 2 dimensional arrays and encryption
    By wdh321 in forum New To Java
    Replies: 0
    Last Post: 08-15-2011, 03:09 PM
  3. Sorting Two Dimensional arrays
    By anfielder in forum New To Java
    Replies: 5
    Last Post: 12-14-2010, 08:12 AM
  4. two dimensional arrays
    By cliffh in forum New To Java
    Replies: 2
    Last Post: 11-06-2010, 12:43 AM
  5. dynamic two dimensional arrays?
    By dinosoep in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •