First of all, the length of the array must be determined when using array initialization, which means that the length of the array is immutable. There are two ways to create an array in JAVA: (1) Static creation such as
Error 1: You are directly creating an empty array params, which means it is empty. The length of the array cannot be changed. If you add data to it at this time, an empty array cannot add anything naturally, so an error will occur during compilation. . You should do the same as the second method I mentioned above, re-create an array with a length of 4, and give the address of the new array to params, that is, Object[] params = null; params = new Object[4]; this way Can be created. Error 2: The initialization of the array can only be added continuously when it is defined. After the definition is completed
Object[] params = null;
params = new Object[4];
After that, if you want to add data to it, you can only add it one by one by subscripting the array.
This declaration method is actually just syntax sugar for Java to initialize the array. The so-called syntax sugar is just for the convenience of programmers. In actual execution, Java will turn it into form 3, which is
Object[] params = new Object[]{1, 2, 3, 4};
Java does not provide syntactic sugar similar to form 2, so it is illegal to use it this way.
A. The author does not need to worry too much about the syntax stipulations B. If you know the array elements in advance, it is more concise to use Object[] params = {1, 2, 3, 4}; to declare the array code. There is no better way to say it.
First of all, the length of the array must be determined when using array initialization, which means that the length of the array is immutable.
There are two ways to create an array in JAVA:
(1) Static creation such as
(2)Dynamic creation like
1
2
3
And the method mentioned by the questioner:
Error 1: You are directly creating an empty array params, which means it is empty. The length of the array cannot be changed. If you add data to it at this time, an empty array cannot add anything naturally, so an error will occur during compilation. . You should do the same as the second method I mentioned above, re-create an array with a length of 4, and give the address of the new array to params, that is, Object[] params = null; params = new Object[4]; this way Can be created.
Error 2: The initialization of the array can only be added continuously when it is defined. After the definition is completed
After that, if you want to add data to it, you can only add it one by one by subscripting the array.
Instead of directly params = {1, 2, 3, 4};
Form 1
This declaration method is actually just syntax sugar for Java to initialize the array. The so-called syntax sugar is just for the convenience of programmers. In actual execution, Java will turn it into form 3, which is
Java does not provide syntactic sugar similar to form 2, so it is illegal to use it this way.
A. The author does not need to worry too much about the syntax stipulations
B. If you know the array elements in advance, it is more concise to use
Object[] params = {1, 2, 3, 4};
to declare the array code. There is no better way to say it.The first way of declaration is incorrect
It should be Object[] params = new Object[length]
The second one is what I usually use