Java 数组
数组的声明、创建、遍历、多维数组与 Arrays 工具类。
声明数组变量
Two equivalent forms: int[] a (preferred, clearer) and int a[] (C style). A declaration creates only the reference — no array exists yet.
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);
}
}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.
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]);
}
}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().
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);
}
}1.9
2.9
3.4
3.5
total = 11.7For-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.
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]);
}
}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.
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);
}
}2
3
[1, 0, 0]
[0, 0, 9]
2Arrays 工具类
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.
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}));
}
}[3, 1, 2]
[1, 2, 3]
[1, 2, 3, 0, 0]
[7, 7, 7]
true声明数组变量
两种写法等价:int[] a(推荐,类型清晰)和 int a[](C 风格)。声明只创建引用,此时数组还不存在,长度是 0 或 null。
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);
}
}3
0
true创建数组
new 类型[长度] 创建后元素自动初始化为默认值(数字 0、布尔 false、引用 null)。也可以用大括号直接给出初值,长度由元素个数决定。
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]);
}
}3 3 2
4
0处理数组
用 for 加下标遍历可以同时拿到索引和值,适合需要修改元素或跳步访问的场景。arr.length 是属性,循环条件里别写成 length()。
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);
}
}1.9
2.9
3.4
3.5
total = 11.7For-Each 循环
for (double v : list) 直接取元素,代码更短且不会越界。但它拿不到下标,也不能在遍历中修改数组元素(改的只是副本)。
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]);
}
}1.9
2.9
3.4
3.5
1多维数组
Java 没有真正的二维数组,只有「数组的数组」。int[][] m = new int[2][3] 创建两行三列;也可以只指定第一维,第二维长度各不相同(锯齿数组)。
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);
}
}2
3
[1, 0, 0]
[0, 0, 9]
2Arrays 工具类
java.util.Arrays 提供 toString 打印、sort 排序、fill 填充、copyOf 复制、equals 比较内容、asList 转列表。直接打印数组只会得到 [I@1b6d3586 这类哈希串。
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}));
}
}[3, 1, 2]
[1, 2, 3]
[1, 2, 3, 0, 0]
[7, 7, 7]
true