Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-20-2008, 09:11 PM
Member
 
Join Date: Feb 2008
Posts: 23
jon80 is on a distinguished road
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?


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)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-21-2008, 03:29 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-21-2008, 05:05 AM
bobleny's Avatar
Member
 
Join Date: Apr 2008
Posts: 36
bobleny is on a distinguished road
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!
__________________
--
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
--
Cheer up, the worst has yet to come...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-21-2008, 09:38 AM
Member
 
Join Date: Feb 2008
Posts: 23
jon80 is on a distinguished road
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>
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-21-2008, 05:12 PM
bobleny's Avatar
Member
 
Join Date: Apr 2008
Posts: 36
bobleny is on a distinguished road
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.
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("--------------------------"); } } }
__________________
--
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
--
Cheer up, the worst has yet to come...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP: Trouble with partial filled arrays daigre7 New To Java 1 04-07-2008 04:05 AM
[SOLVED] How to read a file and compare Array values DonCash Advanced Java 2 04-02-2008 04:22 PM
Interacting with the Java Garbage Collector Java Tip Java Tips 0 03-28-2008 10:04 PM
Java performance Issues (III) - Garbage collection JavaForums Java Blogs 0 01-30-2008 03:01 PM
Garbage Collection JavaForums Java Blogs 0 08-04-2007 04:24 AM


All times are GMT +3. The time now is 08:48 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org