492

I am initializing an array 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 to an error at this line:

data[10] = {10,20,30,40,50,60,71,80,90,91};

How can I solve the problem?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
chatty
  • 5,001
  • 3
  • 19
  • 9

11 Answers11

730
data[10] = {10,20,30,40,50,60,71,80,90,91};

The above is not correct (syntax error). It means you are assigning an array to data[10] which can hold just an element.

If you want to initialize an array, try using Array Initializer:

int[] data = {10,20,30,40,50,60,71,80,90,91};

// or

int[] data;
data = new int[] {10,20,30,40,50,60,71,80,90,91};

Notice the difference between the two declarations. When assigning a new array to a declared variable, new must be used.

Even if you correct the syntax, accessing data[10] is still incorrect (You can only access data[0] to data[9] because index of arrays in Java is 0-based). Accessing data[10] will throw an ArrayIndexOutOfBoundsException.

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 2
    Actually, the primary reason it is incorrect is that it is a syntax error! The AIOB would only happen if the syntax was acceptable. – Stephen C Dec 21 '09 at 04:07
  • 1
    Could you please help me, If I declare an array like this: `public static void product(int[] array){ int[] productArray = new int[array.length];` and want to set all the values of `productArray` **zero**, what should I write? (I think I should write a loop and set all values to zero, is there any better way to do this?) – Hengameh Aug 26 '15 at 13:09
  • 3
    @Hengameh: It's initialized to 0 by default. See http://stackoverflow.com/a/2154340/1000655 – Neal Gokli Mar 06 '17 at 22:56
47

Try

data = new int[] {10,20,30,40,50,60,71,80,90,91 };
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Dean Povey
  • 9,256
  • 1
  • 41
  • 52
  • 1
    +1. You have an extra opening brace. One can also write: data[0] = 10; data[1] = 20; .... after int data[] = new int[10], but it is too much code and it will end up doing the same thing. – Hamish Grubijan Dec 21 '09 at 04:03
30

When you create an array of size 10 it allocated 10 slots but from 0 to 9. This for loop might help you see that a little better.

public class Array {
    int[] data = new int[10]; 
    /** Creates a new instance of an int Array */
    public Array() {
        for(int i = 0; i < data.length; i++) {
            data[i] = i*10;
        }
    }
}
Harel Farkash
  • 157
  • 1
  • 1
  • 12
Bernie Perez
  • 12,513
  • 13
  • 46
  • 55
16

You can do:

int[] data = {10,20,30,40,50,60,71,80,90,91};
13

Syntax

 Datatype[] variable = new Datatype[] { value1,value2.... }

 Datatype variable[]  = new Datatype[] { value1,value2.... }

Example :

int [] points = new int[]{ 1,2,3,4 };
Vinayak
  • 6,056
  • 1
  • 32
  • 30
7

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 *

int n; // size of array here 10
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
    a[i] = Integer.parseInt(s.nextLine()); // using Scanner class
}

Input: 10//array size 10 20 30 40 50 60 71 80 90 91

Displaying data:

for (int i = 0; i < a.length; i++) 
{
    System.out.println(a[i] + " ");
}

Output: 10 20 30 40 50 60 71 80 90 91

Rusty Shackleford
  • 1,111
  • 2
  • 14
  • 19
Py-Coder
  • 2,024
  • 1
  • 22
  • 28
4

You cannot initialize an array like that. In addition to what others have suggested, you can do :

data[0] = 10;
data[1] = 20;
...
data[9] = 91;
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
4

If you want to initialize an array in a constructor, you can't use those array initializer like.

data= {10,20,30,40,50,60,71,80,90,91};

Just change it to

data = new int[] {10,20,30,40,50,60,71,80,90,91};

You don't have to specify the size with data[10] = new int[] { 10,...,91} Just declare the property / field with int[] data; and initialize it like above. The corrected version of your code would look like the following:

public class Array {

    int[] data;

    public Array() {
        data = new int[] {10,20,30,40,50,60,71,80,90,91};
    }

}

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.

Sedat Kilinc
  • 2,843
  • 1
  • 22
  • 20
3

you are trying to set the 10th element of the array to the array try

data = new int[] {10,20,30,40,50,60,71,80,90,91};

FTFY

Morvader
  • 2,317
  • 3
  • 31
  • 44
schubySteve
  • 707
  • 1
  • 4
  • 9
0

Maybe this will work:

public class Array {

    int data[] = new int[10]; 
    /* Creates a new instance of Array */
    public Array() {
        data= {10,20,30,40,50,60,71,80,90,91};
    }
}
trejder
  • 17,148
  • 27
  • 124
  • 216
0
int myArray2[] = new int[3];
int[] myArray = new int[3];
int[] intArray = {13, 14, 15};
int[] intArray = new int[]{7, 8, 9};
int[] intArray = IntStream.range(1, 11).toArray();
int[] intArray = IntStream.rangeClosed(1, 10).toArray();
int[] intArray = IntStream.of(6, 2, 4, 5, 7).toArray();
int[] intArray = IntStream.of(6, 2, 4, 5, 7).sorted().toArray();
Ryan
  • 192
  • 2
  • 4