Java double[][] Issue
I'm doing the following on my code:
double[][] temp=new double[0][2];
The program will run with no runtime exceptions. When I get the length of the temp like this temp.length
it returns 0 and when I tried accessing the length of the inner arrays like this temp[0].length
it always throws an ArrayIndexOutOfBoundsException
. (That was only a test.)
Now I am wondering, Java did create a array with 0 length and at the same time an inner array with a length of 2 in an array with 0 length?
- Did this kind of declaration has implications on memory management?
- Will it develop complications on the coding and running the code?
- Did Java really permit this kind of declaration?
- In what sense did they permit this kind of dec开发者_如何学Claration or did they just overlook this kind of situation?
- And if they permit this declaration does it also has some special uses?
I was just exploring the possibility of doing this kind of declaration and had been questioning myself if this is really permissible.
Your opinions are gladly appreciated.
It is equivalent to
double[][] temp = new double[0][]; // a zero length array of double[]
for(int d=0; d<0; d++)
temp[d] = new double[2]; // whose each element is a new double[2]
of course the loop isn't executed, so there's no waste from "inner array"
see 15.10.1 Run-time Evaluation of Array Creation Expressions (JLS 3 - CHAPTER 15 Expressions)
If an array creation expression contains N DimExpr expressions, then it effectively executes a set of nested loops of depth N-1 to create the implied arrays of arrays. For example, the declaration:
float[][] matrix = new float[3][3];
is equivalent in behavior to:
float[][] matrix = new float[3][]; for (int d = 0; d < matrix.length; d++) matrix[d] = new float[3];
,so
double[][] temp=new double[0][2];
will be equivalent to
double[][] matrix = new double[0][];
for (int d = 0; d < 0; d++)
matrix[d] = new double[2];//would newer hepened
The only valid scenario ,I can think of is where you want to send and empty 2 dimensional array.
double[][] temp = new double[0][0];
return temp;
The above is a valid requirement in many matrix calculations.
Did this kind of declaration has implications on memory management?
Not sure. And might also depends on the JVM to JVM implementations.
Will it develop complications on the coding and running the code?
It should not if you are accessing the array in a loop like this
for(int i = 0; i<temp.length;i++)
for(int j=0; j<temp[i].length;j++)
{
// your code
}
Otherwise if you are accessing directly by using index then you should first check the index bounds.
Did Java really permit this kind of declaration?
Yes. As I have said in the first statement.
In what sense did they permit this kind of declaration or did they just overlook this kind of situation?
As said before: A valid scenario is where you want to send and empty 2 dimensional array There might be other scenarios.
And if they permit this declaration does it also has some special uses? Other than the my last answer I am not sure of any other scenario. But would love to know if they exist.
精彩评论