Initializing an array after the declaration

后端 未结 4 1860
无人共我
无人共我 2021-01-14 03:42

Why we cannot use array intializer after declaring an variable.

For example:

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

But,

int          


        
4条回答
  •  隐瞒了意图╮
    2021-01-14 04:00

    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; i.e. 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

提交回复
热议问题