JAVA重溫拾遺

2013年8月13日18:15:31

exception

  1.   聲明方法後,throws異常,則是逐級拋給調用者,最終,通過聲明main函數的throws實現拋給java運行時系統;

  2. 自定義異常,extends exception,構造函數super(str);

  3. finally 語句不論是否有異常,都會執行,除非之前有exit(-1)出現

  4. 子類的覆蓋方法只能拋出父類拋出的或其子類

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);// ,貌似可以任意修改而不影響輸出的升序結果
}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章