数组是相似类型数据的集合。
例如,如果我们想存储 100 个人的名字,我们可以创建一个字符串类型的数组来存储 100 个名字。
String[] array = new String[100];
在这里,上述数组不能存储超过 100 个名字。Java 数组中的值数量始终是固定的。
如何在 Java 中声明数组?
在 Java 中,我们可以这样声明一个数组。
dataType[] arrayName;
例如,
double[] data;
在这里,data 是一个可以容纳 double
类型值的数组。
但是,这个数组可以容纳多少个元素呢?
好问题!要定义数组可以容纳的元素数量,我们必须在 Java 中为数组分配内存。例如,
// declare an array
double[] data;
// allocate memory
data = new double[10];
在这里,该数组可以存储 **10** 个元素。我们也可以说数组的 **大小或长度** 是 10。
在 Java 中,我们可以在一个语句中声明并分配数组的内存。例如,
double[] data = new double[10];
如何在 Java 中初始化数组?
在 Java 中,我们可以在声明时初始化数组。例如,
//declare and initialize and array
int[] age = {12, 4, 5, 2, 5};
这里,我们创建了一个名为 age 的数组,并使用花括号内的值对其进行了初始化。
请注意,我们没有提供数组的大小。在这种情况下,Java 编译器会自动通过计算数组中的元素数量(即 5)来指定大小。
在 Java 数组中,每个内存位置都与一个数字相关联。这个数字被称为数组索引。我们也可以使用索引号在 Java 中初始化数组。例如,
// declare an array
int[] age = new int[5];
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
..

注意:
- 数组索引始终从 0 开始。也就是说,数组的第一个元素位于索引 0。
- 如果数组的大小为 n,则数组的最后一个元素将位于索引 n-1。
如何在 Java 中访问数组元素?
我们可以使用索引号访问数组的元素。访问数组元素的语法如下:
// access array elements
array[index]
让我们通过一个示例来了解如何使用索引号访问数组元素。
示例:访问数组元素
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};
// access each array elements
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}
输出
Accessing Elements of Array: First Element: 12 Second Element: 4 Third Element: 5 Fourth Element: 2 Fifth Element: 5
在上面的示例中,请注意我们使用索引号来访问数组的每个元素。
我们可以使用循环一次性访问数组的所有元素。
循环遍历数组元素
在 Java 中,我们也可以循环遍历数组的每个元素。例如,
示例:使用 For 循环
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array
// using for loop
System.out.println("Using for Loop:");
for(int i = 0; i < age.length; i++) {
System.out.println(age[i]);
}
}
}
输出
Using for Loop: 12 4 5
在上面的示例中,我们使用 Java 中的 For 循环 来遍历数组的每个元素。请注意循环内的表达式,
age.length
这里,我们使用数组的 length
属性来获取数组的大小。
我们也可以使用 for-each 循环 来遍历数组的元素。例如,
示例:使用 for-each 循环
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array
// using for loop
System.out.println("Using for-each Loop:");
for(int a : age) {
System.out.println(a);
}
}
}
输出
Using for-each Loop: 12 4 5
示例:计算数组元素的总和与平均值
class Main {
public static void main(String[] args) {
int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};
int sum = 0;
Double average;
// access all elements using for each loop
// add each element in sum
for (int number: numbers) {
sum += number;
}
// get the total number of elements
int arrayLength = numbers.length;
// calculate the average
// convert the average from int to double
average = ((double)sum / (double)arrayLength);
System.out.println("Sum = " + sum);
System.out.println("Average = " + average);
}
}
输出:
Sum = 36 Average = 3.6
在上面的示例中,我们创建了一个名为 numbers 的数组。我们使用了 for...each
循环来访问数组的每个元素。
在循环内部,我们计算每个元素的总和。请注意这一行:
int arrayLength = number.length;
这里,我们使用数组的 length 属性 来计算数组的大小。然后我们使用以下公式计算平均值:
average = ((double)sum / (double)arrayLength);
如您所见,我们将 int
值转换为 double
。这在 Java 中称为类型转换。要了解更多关于类型转换的信息,请访问 Java 类型转换。
多维数组
我们到目前为止提到的数组称为一维数组。但是,我们可以在 Java 中声明多维数组。
多维数组是数组的数组。也就是说,多维数组的每个元素本身就是一个数组。例如,
double[][] matrix = {{1.2, 4.3, 4.0},
{4.1, -1.1}
};
这里,我们创建了一个名为 matrix 的多维数组。它是一个二维数组。要了解更多信息,请访问 Java 多维数组。
另请阅读