Results 1 to 8 of 8
  1. #1
    Liq
    Liq is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Lightbulb Three-Dimensional Array

    Im new to java and am reading Sams Teach Yourself Java 6 in 21 Days (5th Edition) which is being really usefull but when he reached the topic of arrays he kind of glossed over 3-D arrays, and im was rather confused. I looked around the internet and did some research on it and i think iunderstand it but i just want to be sure i do. I included source code to the the class.
    Java Code:
    public class ClassNames {
    	public static void main(String[] args) {
    		String names[][][] = new String [5][4][2];
    		//5 rooms
    		//4 tables
    		//2 students per table
    		
    		names [0][0][0] = "John";
    		names [0][0][1] = "Paul";
    		names [0][1][0] = "Johnathan";
    		names [0][1][1] = "Michel";
    		
    		names [4][0][0] = "Christopher";
    		names [4][0][1] = "Peter";
    		names [4][1][0] = "Sam";
    		names [4][1][0] = "Daniel";
    		
    		System.out.println(names[0][0][0] + " is sitting in the First room first desk and is in chair 1 next to his friend " + names[0][0][1]);
    		
    	}
    }
    As you can see i used the 3-D array as 5 rooms( 5 2-D arrays), each classroom had 4 tables(4 rows) and each table had 2 students in it(2 columns).
    Is that usage of a 3-D array correct or am i missing something?
    Last edited by DarrylBurke; 07-04-2013 at 07:11 PM. Reason: Marked Solved

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Three-Dimensional Array

    You can certainly do it that way. I don't use arrays too much. I prefer Lists as they are more flexible. Multiple dimensional arrays can be hard to keep track of so you can manipulate them as follows:

    Java Code:
    public class ArrayExample {
       public static void main(String[] args) {
          String[][][] school = new String[2][][];
          for (int j = 0; j < school.length; j++) {
             String[][] room = new String[2][2];
             for (int k = 0; k < room.length; k++) {
                room[k][0] = "Bill";
                room[k][1] = "Sally";
                school[j] = room;
             }
          }
    
          for (int m = 0; m < school.length; m++) {
             for (int n = 0; n < school[m].length; n++) {
                System.out.println(school[m][n][0]);
                System.out.println(school[m][n][1]);
             }
          }
       }
    }
    Since arrays are reference types the statement
    Java Code:
    String [][][] school = new String[2][][];
    Says that school is a three-D array and has been allocated storage to hold two references. Each reference is to a two-D array which is created and then assigned. When I do use high dimensional arrays I try to break them apart like this because I think it makes the code easier to read.

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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Three-Dimensional Array

    Java doesn't really have multidimensional arrays. What it does have is arrays in which each element is of a specified type, which may be an array type.

    Thus,
    String[] is an array in which each element is of type String.
    String[][] is an array in which each element is of type String[].
    String[][][] is an array in which each element is of type String[][].

    This leads to the interesting possibility of jagged arrays; a String[2][] may contain two elements which are String[10] and String[20].

    If that's not clear, feel free to ask about what you don't understand.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Liq
    Liq is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Three-Dimensional Array

    Quote Originally Posted by DarrylBurke View Post
    Java doesn't really have multidimensional arrays. What it does have is arrays in which each element is of a specified type, which may be an array type.

    Thus,
    String[] is an array in which each element is of type String.
    String[][] is an array in which each element is of type String[].
    String[][][] is an array in which each element is of type String[][].

    This leads to the interesting possibility of jagged arrays; a String[2][] may contain two elements which are String[10] and String[20].

    If that's not clear, feel free to ask about what you don't understand.

    db
    So correct me if im wrong but this is what i understand from your explanation. So for a 2-D array such as String[][] the first [] tells us how many rows there are, and the second [] tells us how many columns there are. Each row consists of a 1-D array, and each column also is a 1-D array. Therefore String[2][4] would create 2 rows(two 1-D arrays), and 4 columns(four 1-D arrays), again correct me if im wrong. From what i understand, and please explain if i am, a 3-D array like String[][][], the first [] tells us how many 2-D arrays to construct, the second [] tells us how many rows for all 2-D arrays created in first bracket, then last [] tells us how many columns will be in all 2-D arrays constructed in first []. Therefore String[5][10][4] would create 10(2-D arrays), then all ten of those 2-D arrays will have 10 rows, and 4 columns. Again if my understanding is wrong please explain.

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: Three-Dimensional Array

    Think of it as "arrays in arrays" instead of rows and columns. So in your first example, String[2][4] would create an array of length 2, and each element in that array is an array of length 4. For a 3D array, you go one step further; String[5][10][4] would create an array of length 5, and each element in that array is an array of length 10, and each element in each of those arrays is an array of length 4.

    EDIT: Consider this piece of code, for instance (not sure if I wrote it myself or copy/pasted it from these forums or somewhere, so I won't take full credit for it):

    Java Code:
    class ArrayTest
    {
      public static void main(String[] args)
      {
        Object[][] testarray=new Object[5][];
        testarray[0]=new String[1];
        testarray[1]=new Integer[2];
        testarray[2]=new Byte[3];
        testarray[3]=new Object[4];
        testarray[4]=new Long[5];
        for (int i=0;i<testarray.length;i++)
        {
          System.out.println(testarray[i].length);
        }
      }
    }
    That would produce the following output:

    Java Code:
    1
    2
    3
    4
    5
    Last edited by Toll; 07-04-2013 at 06:00 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Three-Dimensional Array

    To take it one step further:

    String[2][3][4] tells us that this is an array with 2 elements each of which is a String[3][4]

    String [3][4] in turn tells us that this is an array with 3 elements each of which is a String[4]

    String[4] tells us that this is an array with 4 elements each of which is a String

    There are no rows and columns in a Java 2-d array. It's an array in which each element is an array.

    This code may help your understanding (or not!)
    Java Code:
    int[][][] d3array = new int[2][][];
    for (int i = 0; i < d3array.length; i++) {
      d3array[i] = new int[i + 1][];
      for (int j = 0; j < d3array[i].length; j++) {
        d3array[i][j] = new int[i + j + 1];
        for (int k = 0; k < d3array[i][j].length; k++) {
          d3array[i][j][k] = i + j + k + 1;
        }
      }
    }
    System.out.println(Arrays.deepToString(d3array));
    Arrays.deepToString(...) gives you a human-readable representation of the values in the array.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Liq
    Liq is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Three-Dimensional Array

    Quote Originally Posted by Toll View Post
    Think of it as "arrays in arrays" instead of rows and columns.

    I think this was my main source of confusion, it makes much more sense as arrays of arrays, Thankyou, and to everyone who posted thank you also, i now understand much better arrays.
    -Now just one last question how do i mark a thread as answered?
    Last edited by Liq; 07-04-2013 at 06:48 PM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Three-Dimensional Array

    Quote Originally Posted by Liq View Post
    -Now just one last question how do i mark a thread as answered?
    I've done it for you.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. two dimensional array
    By grace425 in forum New To Java
    Replies: 2
    Last Post: 02-23-2013, 01:01 AM
  2. Help with Two Dimensional Array
    By johnjacob in forum New To Java
    Replies: 2
    Last Post: 06-03-2011, 09:22 PM
  3. Two dimensional array
    By niu_niu in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 01:34 AM
  4. about two dimensional array
    By matin1234 in forum New To Java
    Replies: 2
    Last Post: 06-01-2010, 12:09 PM
  5. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 08:21 PM

Tags for this Thread

Posting Permissions

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