目录
1、异常机制本质
2、异常的处理
3、数组的声明
4、数组的初始化
5、数组的遍历
6、数组的拷贝
7、java.util.Arrays类
/**IllegalAgeException:非法年龄异常,继承Exception类*/ class IllegalAgeException extends Exception { //默认构造器 public IllegalAgeException() { } //带有详细信息的构造器,信息存储在message中 public IllegalAgeException(String message) { super(message); } }
//创建基本类型一维数组 public class Test { public static void main(String args[]) { int[] s = null; // 声明数组; s = new int[10]; // 给数组分配空间; for (int i = 0; i < 10; i++) { s[i] = 2 * i + 1;//给数组元素赋值; } } }
//创建引用类型一维数组 class Man{ private int age; private int id; public Man(int id,int age) { super(); this.age = age; this.id = id; } } public class Test { public static void main(String[] args) { Man[] mans; //声明引用类型数组; mans = new Man[10]; //给引用类型数组分配空间; Man m1 = new Man(1,11); Man m2 = new Man(2,22); mans[0]=m1;//给引用类型数组元素赋值; mans[1]=m2;//给引用类型数组元素赋值; } }
int[] a = { 1, 2, 3 };// 静态初始化基本类型数组; Man[] mans = { new Man(1, 1), new Man(2, 2) };// 静态初始化引用类型数组;
int[] a1 = new int[2];//动态初始化数组,先分配空间; a1[0]=1;//给数组元素赋值; a1[1]=2;//给数组元素赋值;
int a2[] = new int[2]; // 默认值:0,0 boolean[] b = new boolean[2]; // 默认值:false,false String[] s = new String[2]; // 默认值:null, null
public class Test { public static void main(String[] args) { String[] ss = { "aa", "bbb", "ccc", "ddd" }; for (String temp : ss) { System.out.println(temp); } } }
public class Test { public static void main(String args[]) { String[] s = {"A","B","C","D","E"}; String[] sBak = new String[6]; System.arraycopy(s,0,sBak,0,s.length);//数组的拷贝 } }