How to initialize an array in Java?

JavaArraysInitialization

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

Try

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

Solution 3 - Java

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;
        }
    }
}

Solution 4 - Java

You can do:

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

Solution 5 - Java

Syntax

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

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

Example :

int [] points = new int[]{ 1,2,3,4 };

Solution 6 - Java

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

Solution 7 - Java

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;

Solution 8 - Java

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.

Solution 9 - Java

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

Solution 10 - Java

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};
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchattyView Question on Stackoverflow
Solution 1 - JavaPrasoon SauravView Answer on Stackoverflow
Solution 2 - JavaDean PoveyView Answer on Stackoverflow
Solution 3 - JavaBernie PerezView Answer on Stackoverflow
Solution 4 - Javauser2066936View Answer on Stackoverflow
Solution 5 - JavaVinayakView Answer on Stackoverflow
Solution 6 - JavaShivanandamView Answer on Stackoverflow
Solution 7 - JavafastcodejavaView Answer on Stackoverflow
Solution 8 - JavaSedat KilincView Answer on Stackoverflow
Solution 9 - JavaschubySteveView Answer on Stackoverflow
Solution 10 - JavaSajjad BehraveshView Answer on Stackoverflow