简体   繁体   中英

Initializing an array after the declaration

Why we cannot use array intializer after declaring an variable.

For example:

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

But,

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

is correct. Is there any way to use array initialize after declaring an variable.

This is how you can.

int arr[];

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

Because an array doesn't work like that in java.

int arr[4];
arr[0] = 1; 
arr[1] = 2; 
arr[2] = 3; 
arr[3] = 4;

There are three steps to creating an array, declaring it, allocating it and initializing it.

Declaring Arrays Like other variables in Java, an array must have a specific type like byte, int, String or double. Only variables of the appropriate type can be stored in an array. You cannot have an array that will store both ints and Strings, for instance.

Like all other variables in Java an array must be declared. When you declare an array variable you suffix the type with [] to indicate that this variable is an array. Here are some examples:

int[] k; float[] yt; String[] names;

In other words you declare an array like you'd declare any other variable except you append brackets to the end of the variable type.

Allocating Arrays Declaring an array merely says what it is. It does not create the array. To actually create the array (or any other object) use the new operator. When we create an array we need to tell the compiler how many elements will be stored in it. Here's how we'd create the variables declared above:

k = new int[3]; yt = new float[7]; names = new String[50];

The numbers in the brackets specify the dimension of the array; ie how many slots it has to hold values. With the dimensions above k can hold three ints, yt can hold seven floats and names can hold fifty Strings. Therefore this step is sometimes called dimensioning the array. More commonly this is called allocating the array since this step actually sets aside the memory in RAM that the array requires.

This is also our first look at the new operator. new is a reserved word in java that is used to allocate not just an array, but also all kinds of objects. Java arrays are full-fledged objects with all that implies. For now the main thing it implies is that we have to allocate them with new.

Initializing Arrays Individual elements of the array are referenced by the array name and by an integer which represents their position in the array. The numbers we use to identify them are called subscripts or indexes into the array. Subscripts are consecutive integers beginning with 0. Thus the array k above has elements k[0], k[1], and k[2]. Since we started counting at zero there is no k[3], and trying to access it will generate an ArrayIndexOutOfBoundsException.

You can use array elements wherever you'd use a similarly typed variable that wasn't part of an array.

Here's how we'd store values in the arrays we've been working with:

k[0] = 2; k[1] = 5; k[2] = -2; yt[6] = 7.5f; names[4] = "Fred";

We can even declare, allocate, and initialize an array at the same time providing a list of the initial values inside brackets like so:

int[] k = {1, 2, 3}; float[] yt = {0.0f, 1.2f, 3.4f, -9.87f, 65.4f, 0.0f, 567.9f};

see http://www.cafeaulait.org/javatutorial.html#xtocid499429

Check this

example :-

int data[] = new int[] {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 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM