On Mar 20, 5:00 am, [email protected] wrote: > Thanks Martin. > > I appreciate the answer, but where did you come up with it? I can't find any > documentation on this and was wondering if I missed something, somewhere. You may find an explanation here: http://math.hws.edu/javanotes/c7/s5.html (and by the way this url explains a lot of things in details).
Michèle > > Cheers. > Rob > > > > -----Original Message----- > From: [email protected] > To: [email protected] > > Cc: [email protected] > Sent: Thu, Mar 11, 2010 8:42 pm > Subject: Re: [java programming] Lab 1036: Three Dimensional array question > > Hi Rob: > > > 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? > > > // 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); > > To fix this change your print this section to > > //Show the first dimension array length > System.out.println("ages.length = " + ages.length); > //Show the second dimension array length > System.out.println("ages[0].length = " + ages[0].length); > //Show the third dimension array length > System.out.println("ages[0][0].length = " + ages[0][0].length); > > Can you see the difference? > > Bye > > Martin H. > > > 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]; > > > 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 > athttp://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 To unsubscribe from this group, send email to javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
