2013年8月13日18:15:31
exception
声明方法后,throws异常,则是逐级抛给调用者,最终,通过声明main函数的throws实现抛给java运行时系统;
自定义异常,extends exception,构造函数super(str);
子类的覆盖方法只能抛出父类抛出的或其子类
2013年8月14日18:07:50
public class StringTest { public static void main(String[] args) throws CloneNotSupportedException { /* Integer in=new Integer(12); int i=in.intValue(); System.out.println(i+1); String is=in.toString(); i=Integer.valueOf(is); System.out.println(i); System.out.println(is+".");//包装类的开封与包装 */ // String和Stringbuffer用法 /* StringBuffer sb=new StringBuffer("hello"); sb.append(5).append('w').append(3.14).append("world"); System.out.println(sb.toString());//auto change System.out.println(sb); sb.insert(5, "world"); System.out.println(sb); sb.delete(5, 10); System.out.println(sb); */ Professor p = new Professor(55, "lishi"); Student s1 = new Student(22, "zhangshan", p); Student s2 = (Student) s1.clone(); s2.name = "jim"; s2.num = 123; s2.p1.age = 321; s2.p1.name = "joshua"; s1.print(); s2.print(); } } class Professor implements Cloneable { int age; String name; public Professor(int i, String s) { this.age = i; this.name = s; // TODO Auto-generated constructor stub } public Professor clone() throws CloneNotSupportedException { return (Professor) super.clone(); } public String print() { return (" p_name:" + this.name + " p_age:" + this.age); } } class Student implements Cloneable { int num; String name; Professor p1; public Student(int i, String n, Professor p) { // TODO Auto-generated constructor stub this.name = n; this.num = i; this.p1 = p; } public Student clone() throws CloneNotSupportedException { Student o = null; try { o = (Student) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } o.p1 = p1.clone(); return o;// 不处理就抛出去让上级处理 /* * this.p1=p1.clone(); return super.clone(); */ } public void print() { System.out.println("name: " + this.name + "number: " + this.num + this.p1.print()); } }
2013年8月15日18:42:28
数组的复制、排序、查找
对象数组的自定义排序
import java.util.*; public class ArragTest { public static void main(String[] args) { /* * int[] arr1 = { 3, 2, 1, 0 }; int[] arr2 = new int[5]; * System.arraycopy(arr1, 0, arr2, 1, arr1.length);// 从arr2的第1索引位开始 */ /* * //System.out.println(arr1); for(int i=0;i<arr1.length;i++){ * System.out.println(arr1[i]); } Arrays.sort(arr1); for(int * i=0;i<arr1.length;i++){ System.out.println(arr1[i]); } */ /* * for(int i=0;i<arr2.length;i++){ System.out.println(arr2[i]); } * Arrays.sort(arr2);//必须先排序后才能查找 int index=Arrays.binarySearch(arr2, * 3);//搜索 System.out.println(index); System.out.println(arr2[index]); */ Students[] s1 = { new Students("joshua", 33), new Students("join", 22), new Students("jim", 11) }; Students[] s2 = new Students[4];// 对象数组的声明方法 System.arraycopy(s1, 0, s2, 0, s1.length); for (int i = 0; i < s2.length; i++) { System.out.println(s2[i]); } Arrays.sort(s1); for (int i = 0; i < s1.length; i++) { System.out.println(s1[i]); } int index1 = Arrays.binarySearch(s1, new Students("joshua", 33)); System.out.print(index1 + "\t"); System.out.println(s1[index1]); } } class Students implements Comparable { private int num; private String name; public Students(String s, int n) { this.name = s; this.num = n; // TODO Auto-generated constructor stub } public String toString() { return ("name= " + this.name + " num= " + this.num); } @Override public int compareTo(Object o) { // TODO Auto-generated method stub Students s = (Students) o; return num > s.num ? 1 : (num == s.num ? 0 : -1);// ,貌似可以任意修改而不影响输出的升序结果 } }