|
![]() |
Joanne
Ted Scofield wrote:
...
the length of a matrix is calculated only once whereas in your case, the matrix length will be calculated in every iteration.
...
Campbell Ritchie wrote:If you are using matrices, it is worthwhile checking that the array is "rectangular".
When the array is presented to your constructor, iterate through it and check that the length of each element is the same. If you need a square array, that would require an additional check for the first element, then all the elements must have the same length. Beware in case you ever encounter a 0-length array!
Arrays, Strings (which actually hide an array in their depths) and Lists all have a field (directly or indirectly) which measures how many elements they contain. In the case of arrays and Strings, it never changes, but in Lists it does change. The String#length() and List#size() methods simply return that field, without having to do any more calculations. The List size field is recalculated whenever elements are added or removed.
Fred Hamilton wrote:Since matrix is defined as int[][], wouldn't that automatically guarantee that the array is rectangular, ie all rows have the same length and all cols have the same length? Am i missing something?
SCJA
~Currently preparing for SCJP6
Brian Legg wrote:
Fred Hamilton wrote:Since matrix is defined as int[][], wouldn't that automatically guarantee that the array is rectangular, ie all rows have the same length and all cols have the same length? Am i missing something?
There is no such thing as a multi-dimensional array in Java, just an Array of Arrays. That being said with a definition of int[][] you could assign an int array of size 10 to element 0 and an int array of size 200 to element 1 and so on. As long as each array is an array of ints then it can be assigned to an element in an array of int arrays. Basically you are creating an standard array which holds integer arrays, each integer array does not need to be the same size.
SCJA
~Currently preparing for SCJP6
Hold that thought. Tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
|