Hi Rob
As you create int[][][] ages = new int[10][5][3], ages.length = 10,
ages[i].length = 5 always, for any i from 0 to 9 and ages[i][j].length =
3 for any i and j.
I think you want to println ages.length, ages[0].length and
ages[0][0].length and not ages[2].length.
Hope it helps
mihai
[email protected] a écrit :
For the JAVA array homework, on Exercise 1.2, step 4, I have the
following to create a three dimensional array of int and print the
output, I cannot print out the ages[2].length variable correctly.
I've initialized the ages array with the initial values of 10, 5 & 3,
but as you can see from the output, ages[2].length always equals to
the value of ages[1].length. Any suggestions on how I can fix this?
Cheers.
Rob B.
public class JavaThreeDimensionArray {
/** Creates a new instance of JavaTwoDimensionArray */
public JavaThreeDimensionArray() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Declare and create three dimensional int array whose size
is 10 by 5 by 3
int[][][] ages = new int[10][5][3];
// Display the number of rows and columns and shelves
System.out.println("ages.length = " + ages.length);
System.out.println("ages[1].length = " + ages[1].length);
System.out.println("ages[2].length = " + ages[2].length);
int startnum = 100;
// Display the value of each entry in the array
for( int i=0; i<ages.length; i++ ){
System.out.println("\nStarting row " + i);
for( int j=0; j<ages[i].length; j++ ){
for( int k=0; k<ages[i][j].length; k++ ){
ages[i][j][k] = startnum ;
startnum = startnum +1 ;
System.out.print( ages[i][j][k] + " " );
}
}
}
}
}
Output:
run:
ages.length = 10
ages[1].length = 5
ages[2].length = 5
Starting row 0
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
Starting row 1
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
Starting row 2
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
Starting row 3
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
Starting row 4
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
Starting row 5
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
Starting row 6
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
Starting row 7
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
Starting row 8
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
Starting row 9
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 BUILD
SUCCESSFUL (total time: 3 seconds)
--
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en