Ⅰ. 一维数组
1. 声明
(1)int
int[] test_int;
(2)String
String[] test_String;
(3)自定义类
MyClass[] test_mc;
(4)对象数组
Integer[] test_Integer;
2. 初始化
A. 使用关键字new进行定义
类型标识符[] 数组名 = new 类型标识符[数组长度];
(1)int
产生一个具有10个单元,类型为 int 的数组对象,所有单元的初值为0
int[] test_int = new int[10];
或
int[] test_int;
test_int = new int[10];
(2)String
产生一个具有10个单元,类型为 String 的数组对象,所有单元的初值为null
String[] test_String = new String[10];
或
String[] test_String;
test_String = new String[10]; // 注意:不要写成 new String(10)
(3)自定义类
产生一个具有10个单元,类型为 MyClass 的数组对象,所有单元的初值为null
MyClass[] test_mc = new MyClass[10];
或
MyClass[] test_mc;
test_mc = new MyClass[10];
(4)对象数组
产生一个具有10个单元,类型为 Integer 的数组对象,所有单元的初值为null
Integer[] test_Integer = new Integer[10];
或
Integer[] test_Integer;
test_Integer = new Integer[10];
B. 直接在声明的时候进行初始化
注意:这种定义方式只能在一行代码中,不能分开。
(1)int
int[] test_int = {1,2,3};
(2)String
String[] test_String = {"ab","cd","ef"};
或
String[] test_String = {new String("ab"),new String("cd"),new String("ef")};
(3)自定义类
MyClass test_mc1 = new MyClass();
MyClass test_mc2 = new MyClass();
MyClass test_mc3 = new MyClass();
MyClass[] test_mc = {test_mc1,test_mc2,test_mc3};
(4)对象数组
Integer[] test_Integer = {3,5};
或
Integer[] test_Integer = {new Integer(3), new Integer(5)};
C. 采用如下方法定义及初始化
(1)int
int[] test_int = new int[] {1,2,3};
(2)String
String[] test_String = new String[] {"ab","cd","ef"};
或
String[] test_String = new String[] {new String("ab"), new String("cd"), new String("ef")};
(3)自定义类
MyClass test_mc1 = new MyClass();
MyClass test_mc2 = new MyClass();
MyClass test_mc3 = new MyClass();
MyClass[] test_mc = new MyClass[] {test_mc1,test_mc2,test_mc3};
(4)对象数组
Integer[] test_Integer = new Integer[] {1,2,3};
或
Integer[] test_Integer = new Integer[] {new Integer(1), new Integer(2), new Integer(3)};
3. 使用foreach语句遍历一维数组
(1)int
public class MyClass {
public static void main(String[] args) {
int[] test_int = new int[10];
test_int[0] = 1;
for(int x: test_int) {
System.out.println(x + " ");
}
}
}
输出:
(2)String
public class MyClass {
public static void main(String[] args) {
String[] test_String = {"ab","cd","ef"};
for(String x: test_String) {
System.out.print(x + " ");
}
}
}
输出:
(3)自定义类
public class MyClass {
public int value = 1;
public static void main(String[] args) {
MyClass test_mc1 = new MyClass();
MyClass test_mc2 = new MyClass();
MyClass test_mc3 = new MyClass();
MyClass[] test_mc = {test_mc1,test_mc2,test_mc3};
for(MyClass x: test_mc) {
System.out.print(x.value + " ");
}
System.out.println();
for(MyClass x: test_mc) {
System.out.print(x + " ");
}
}
}
输出:
(4)对象数组
public class MyClass {
public int value = 1;
public static void main(String[] args) {
Integer[] test_Integer = {new Integer(3), new Integer(5)};
for(Integer x: test_Integer) {
System.out.print(x + " ");
}
}
}
输出:
Ⅱ. 二维数组
1. 声明
(1)int
int[][] test_int;
(2)String
String[][] test_String;
(3)自定义类
MyClass[][] test_mc;
(4)对象数组
Integer[][] test_Integer;
2. 初始化
数组名 = new 类型说明符[数组长度][];
数组名 = new 类型说明符[数组长度][数组长度];
对于没有初始化的维度,其值为null:
int arr[][];
arr = new int[3][4];
其相当于下述4条语句:
arr = new int[3][]; // 创建一个有3个元素的数组,且每个元素也是一个数组
arr[0] = new int[4]; // 创建arr[0]元素的数组,它有4个元素
arr[1] = new int[4]; // 创建arr[1]元素的数组,它有4个元素
arr[2] = new int[4]; // 创建arr[2]元素的数组,它有4个元素
其等价于:
arr = new int[3][];
for(int i=0;i<3;++i) arr[i] = new int[4];
对于二维数组,当只定义一维数组时,另一维的维数可以不一样,也就是说不一定是规则的矩阵形式。如:
int[][] arr;
arr = new int[3][];
arr[0] = new int[3];
arr[1] = new int[2];
arr[2] = new int[1];
3. 使用foreach语句遍历二维数组
for(int[] arr1: arr){
for(int x: arr1){
System.out.print(x + " ");
}
System.out.println();
}