简体   繁体   English

声明后初始化数组

[英]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. 因为数组不能像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. 声明数组像Java中的其他变量一样,数组必须具有特定的类型,例如byte,int,String或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. 像Java中的所有其他变量一样,必须声明一个数组。 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; int [] k; float[] yt; 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. 要实际创建数组(或任何其他对象),请使用new运算符。 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]; k =新的int [3]; yt = new float[7]; yt =新的float [7]; names = new String[50]; 名称=新的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. 尺寸大于k时,可以容纳三个整数,yt可以容纳七个浮点数,名称可以容纳五十个字符串。 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. 通常,这称为分配阵列,因为此步骤实际上将阵列所需的RAM中的内存留出了。

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. new是java中的保留字,它不仅用于分配数组,而且还分配各种对象。 Java arrays are full-fledged objects with all that implies. Java数组是具有所有含义的成熟对象。 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]. 下标是从0开始的连续整数。因此,上面的数组k具有元素k [0],k [1]和k [2]。 Since we started counting at zero there is no k[3], and trying to access it will generate an ArrayIndexOutOfBoundsException. 因为我们开始从零开始计数,所以没有k [3],尝试访问它会生成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 [0] = 2; k[1] = 5; k [1] = 5; k[2] = -2; k [2] = -2; yt[6] = 7.5f; yt [6] = 7.5f; names[4] = "Fred"; names [4] =“弗雷德”;

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}; int [] k = {1,2,3}; float[] yt = {0.0f, 1.2f, 3.4f, -9.87f, 65.4f, 0.0f, 567.9f}; float [] yt = {0.0f,1.2f,3.4f,-9.87f,65.4f,0.0f,567.9f};

see http://www.cafeaulait.org/javatutorial.html#xtocid499429 参见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 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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