Results 1 to 6 of 6
- 04-20-2008, 07:11 PM #1
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)
- 04-21-2008, 01:29 AM #2
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
- 04-21-2008, 03:05 AM #3
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...
- 04-21-2008, 07:38 AM #4
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>
- 04-21-2008, 03:12 PM #5
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...
-
Similar Threads
-
HELP: Trouble with partial filled arrays
By daigre7 in forum New To JavaReplies: 1Last Post: 04-07-2008, 02:05 AM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM -
Interacting with the Java Garbage Collector
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks