Java 数组

数组的声明、创建、遍历、多维数组与 Arrays 工具类。

Beginner
Key Points数组长度固定、创建后不可变;下标从 0 开始,越界抛异常;打印用 Arrays.toString,排序用 Arrays.sort。

声明数组变量

Two equivalent forms: int[] a (preferred, clearer) and int a[] (C style). A declaration creates only the reference — no array exists yet.

Java
public class Declare {
    public static void main(String[] args) {
        int[] a;
        double[] b;
        String[] c;

        a = new int[3];
        System.out.println(a.length);
        System.out.println(a[0]);

        String[] d = null;
        System.out.println(d == null);
    }
}
Output
3
0
true

创建数组

new Type[n] fills the array with defaults: 0 for numbers, false for boolean, null for references. A brace initialiser sets both values and length at once.

Java
public class Create {
    public static void main(String[] args) {
        int[] a = new int[3];
        int[] b = {1, 2, 3};
        int[] c = new int[]{4, 5};

        System.out.println(a.length + " " + b.length + " " + c.length);
        System.out.println(b[0] + b[2]);
        System.out.println(a[1]);
    }
}
Output
3 3 2
4
0

处理数组

A for loop with an index gives you both position and value, which you need when mutating elements or stepping. arr.length is a field — never length().

Java
public class Process {
    public static void main(String[] args) {
        double[] list = {1.9, 2.9, 3.4, 3.5};

        for (int i = 0; i < list.length; i++) {
            System.out.println(list[i] + " ");
        }

        double total = 0;
        for (int i = 0; i < list.length; i++) {
            total += list[i];
        }
        System.out.println("total = " + total);
    }
}
Output
1.9 
2.9 
3.4 
3.5 
total = 11.7

For-Each 循环

for (double v : list) yields elements directly — shorter and safe from index errors. The trade-off: no index, and assigning to v does not change the array.

Java
public class ForEachArr {
    public static void main(String[] args) {
        double[] list = {1.9, 2.9, 3.4, 3.5};

        for (double v : list) {
            System.out.println(v);
        }

        int[] nums = {1, 2, 3};
        for (int n : nums) {
            n = n * 2;
        }
        System.out.println(nums[0]);
    }
}
Output
1.9
2.9
3.4
3.5
1

多维数组

Java has no true 2-D arrays, only arrays of arrays. int[][] m = new int[2][3] makes two rows of three; you may also size only the first dimension, giving jagged rows.

Java
import java.util.Arrays;

public class MultiDim {
    public static void main(String[] args) {
        int[][] m = new int[2][3];
        m[0][0] = 1;
        m[1][2] = 9;

        System.out.println(m.length);
        System.out.println(m[0].length);
        System.out.println(Arrays.toString(m[0]));
        System.out.println(Arrays.toString(m[1]));

        int[][] jag = {{1}, {2, 3}};
        System.out.println(jag[1].length);
    }
}
Output
2
3
[1, 0, 0]
[0, 0, 9]
2

Arrays 工具类

java.util.Arrays offers toString for printing, sort, fill, copyOf, equals for content comparison and asList. Printing an array directly only yields something like [I@1b6d3586.

Java
import java.util.Arrays;

public class ArraysUtil {
    public static void main(String[] args) {
        int[] a = {3, 1, 2};

        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));

        int[] b = Arrays.copyOf(a, 5);
        System.out.println(Arrays.toString(b));

        int[] c = new int[3];
        Arrays.fill(c, 7);
        System.out.println(Arrays.toString(c));

        System.out.println(Arrays.equals(a, new int[]{1, 2, 3}));
    }
}
Output
[3, 1, 2]
[1, 2, 3]
[1, 2, 3, 0, 0]
[7, 7, 7]
true

Friends & Partners