I am initializing an array data like this :
public class Array {
int data[] = new int[10];
/** Creates a new instance of Array */
public Array() {
data[10] = {10,20,30,40,50,60,71,80,90,91};
}
}
NetBeans points an error at the line
data[10] = {10,20,30,40,50,60,71,80,90,91};
How can I solve the problem?
Rather than learning un-Official websites learn from oracle website
link follows:Click here
*You can find Initialization as well as declaration with full description *
Input: 10//array size 10 20 30 40 50 60 71 80 90 91
Displaying data:
Output: 10 20 30 40 50 60 71 80 90 91
Try
data = new int[] {10,20,30,40,50,60,71,80,90,91 };
you are trying to set the 10th element of the array to the array try
FTFY
You can do:
If you want to initialize an array in a constructor, you can't use those array initializer like.
Just change it to
You don't have to specify the size with
data[10] = new int[] { 10,...,91}
Just declare the property / field withint[] data;
and initialize it like above. The corrected version of your code would look like the following:As you see the bracket are empty. There isn't any need to tell the size between the brackets, because the initialization and its size are specified by the count of the elements between the curly brackets.
You cannot initialize an array like that. In addition to what others have suggested, you can do :