October 01, 2005 Rectangular Array Initialization | ||||
---|---|---|---|---|
| ||||
I've searched the archive and couldn't see a working solution for how to initialize a rectangular/multi-dimensional array. I get an ArrayBoundsError when attempting to access it. Here's my code: float[][] matrix = new float[][5]; for (int i = 0; i < 5; i++) { matrix[i].length = 5; } matrix[0][0] = 1f; matrix[1][1] = 1f; // ArrayBoundsError here However, if I change my code as follow, it works. matrix[0][0] = 1f; matrix[1].length = 5; matrix[1][1] = 1f; Haven't I already set the length of the array in my for loop earlier? What's going on? Thanks. |
October 01, 2005 Re: Rectangular Array Initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | "John C" <johnch_atms@hotmail.com> wrote in message news:dhlsu7$652$1@digitaldaemon.com... > I've searched the archive and couldn't see a working solution for how to initialize a rectangular/multi-dimensional array. I get an ArrayBoundsError when attempting to access it. > > Here's my code: > > float[][] matrix = new float[][5]; > for (int i = 0; i < 5; i++) { > matrix[i].length = 5; > } > matrix[0][0] = 1f; > matrix[1][1] = 1f; // ArrayBoundsError here > > However, if I change my code as follow, it works. > > matrix[0][0] = 1f; > matrix[1].length = 5; > matrix[1][1] = 1f; > > Haven't I already set the length of the array in my for loop earlier? What's going on? > > Thanks. Ok, ignore this. It was a typo in my code. |
Copyright © 1999-2021 by the D Language Foundation