Results 1 to 6 of 6
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default 3d array filled with garbage values

    I'm trying to code a 3D array, however, garbage values are being displayed even for the 1st dimension.

    Any ideas, pls?
    :confused:

    Code
    /**
    * @author User
    *
    */
    public class ArrayPlayground {

    /**
    * @param args
    */
    public static void main(String[] args) {
    final int X_YEARS = 5;
    final int Y_SHOPS = 10;
    final int Z_CATEGORY = 6;

    int [][][] myCube = new int [X_YEARS] [Y_SHOPS] [Z_CATEGORY];

    //X_YEARS updated from 1985 to 1990

    int startYear = 1985;
    for (int firstRow = 0; firstRow < (X_YEARS - 1); firstRow++) {
    myCube[firstRow][0][0] = startYear;
    startYear++;
    }
    // Display values
    for (int X = 0; X < (X_YEARS - 1); X++) {
    for (int Y = 0; Y < (Y_SHOPS - 1); Y++) {
    for (int Z = 0; Z < (Z_CATEGORY - 1); Z++) {
    System.out.println("Year: " + myCube[X]);
    System.out.println("Shop: " + myCube[Y]);
    System.out.println("Category: " + myCube[Z]);
    System.out.println("--------------------------");
    }
    }
    }
    }


    }

    Output:
    Year: [[I@3e25a5
    Shop: [[I@3e25a5
    Category: [[I@3e25a5
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@3e25a5
    Category: [[I@19821f
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@3e25a5
    Category: [[I@addbf1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@3e25a5
    Category: [[I@42e816
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@3e25a5
    Category: [[I@9304b1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@19821f
    Category: [[I@3e25a5
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@19821f
    Category: [[I@19821f
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@19821f
    Category: [[I@addbf1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@19821f
    Category: [[I@42e816
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@19821f
    Category: [[I@9304b1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@addbf1
    Category: [[I@3e25a5
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@addbf1
    Category: [[I@19821f
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@addbf1
    Category: [[I@addbf1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@addbf1
    Category: [[I@42e816
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@addbf1
    Category: [[I@9304b1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@42e816
    Category: [[I@3e25a5
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@42e816
    Category: [[I@19821f
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@42e816
    Category: [[I@addbf1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@42e816
    Category: [[I@42e816
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@42e816
    Category: [[I@9304b1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@9304b1
    Category: [[I@3e25a5
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@9304b1
    Category: [[I@19821f
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@9304b1
    Category: [[I@addbf1
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@9304b1
    Category: [[I@42e816
    --------------------------
    Year: [[I@3e25a5
    Shop: [[I@9304b1
    Category: [[I@9304b1
    --------------------------
    Year: [[I@3e25a5
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at ArrayPlayground.main(ArrayPlayground.java:34)

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    will you please discuss us about your program? What's the purpose of this program?

    And also don't forget to post your codes with codetag....

    regards,
    sukatoa

  3. #3
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

    Multidimensional arrays can get harry fast!

    System.out.println("Year: " + myCube[X][0][0]); Will print out the year.
    The other "System.out...." lines don't yet have data so they won't print any data.


    When System.out.println() is given an array or an object, it will simply print out it's memory location. Hence the wired data that was outputted; an array is stored at this location:
    myCube[X]

    A year is stored at this location:
    myCube[X][0][0]


    Hope this helps!
    -- www.firemelt.net --
    Cheer up, the worst has yet to come...

  4. #4
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    I was trying to simulate a 3d array (cube) say for a pivot chart.

    <code>
    /**
    *
    */

    /**
    * @author User
    *
    */
    public class ArrayPlayground {

    /**
    * @param args
    */
    public static void main(String[] args) {
    final int X_YEARS = 5;
    final int Y_SHOPS = 10;
    final int Z_CATEGORY = 6;

    int [][][] myCube = new int [X_YEARS] [Y_SHOPS] [Z_CATEGORY];

    //X_YEARS updated from 1985 to 1990

    int startYear = 1985;
    for (int firstRow = 0; firstRow < (X_YEARS - 1); firstRow++) {
    myCube[firstRow][0][0] = startYear;
    startYear++;
    }
    // Display values
    for (int X = 0; X < (X_YEARS - 1); X++) {
    for (int Y = 0; Y < (Y_SHOPS - 1); Y++) {
    for (int Z = 0; Z < (Z_CATEGORY - 1); Z++) {
    System.out.println("Year: " + myCube[X]);
    System.out.println("Shop: " + myCube[Y]);
    System.out.println("Category: " + myCube[Z]);
    System.out.println("--------------------------");
    }
    }
    }
    }


    }
    </code>

  5. #5
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

    lol, the code tags should use a square bracket instead "[cod...]"

    If I have time I will draw a pretty picture of your array to help you out.

    This will store 5 years and display the data.
    Java Code:
    public class ArrayPlayground
    {
        public static void main(String[] args)
        {
            int X_YEARS = 5;
            int Y_SHOPS = 10;
            int Z_CATEGORY = 6;
    
            X_YEARS++;
            Y_SHOPS++;
            Z_CATEGORY++;
    
            // When you declare an array, java wants to know how many
            //     "items" you want to store.
            //     You want to store 6 for x; 1985-1990;
            int [][][] myCube = new int [X_YEARS] [Y_SHOPS] [Z_CATEGORY];
    
            //X_YEARS updated from 1985 to 1990
            int startYear = 1985;
            for (int firstRow = 0; firstRow < X_YEARS; firstRow++)
            {
                myCube[firstRow][0][0] = startYear;
                startYear++;
            }
            
            // Display values
            for (int X = 0; X < X_YEARS; X++)
            {
                System.out.println("Year: " + myCube[X][0][0]);
                System.out.println("--------------------------");
            }
        }
    }
    -- www.firemelt.net --
    Cheer up, the worst has yet to come...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. HELP: Trouble with partial filled arrays
    By daigre7 in forum New To Java
    Replies: 1
    Last Post: 04-07-2008, 03:05 AM
  2. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 PM
  3. Interacting with the Java Garbage Collector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 09:04 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
  •