1.arraycopy(來源陣列,來源陣列複製起點,目標陣列,目標陣列填入的起點,複製的項數)
2.複製純量陣列:arraycopy方法會複製出一個完全獨立的陣列
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class arraycopy1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a1[] = {1,2,3};
int a2[] = new int[3];
//複製陣列
System.arraycopy(a1, 0, a2, 0, a1.length);
//更改陣列a2第二個元素
a2[1] = 9;
//顯示a1的內容
System.out.print("陣列a1的元素: ");
for (int i=0;i<a1.length;i++) {
System.out.print(a1[i] + " ");
}
//顯示a2的內容
System.out.print(" 陣列a2的元素 ");
for (int i=0;i<a2.length;i++) {
System.out.print(a2[i] + " ");
}
System.out.println("");
}
}
sample067-2 複製陣列2
1.複製物件陣列:兩個陣列都會改變
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class arraycopy2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//可以改變使用stringbuffer
StringBuffer a1[] = new StringBuffer[3];
StringBuffer a2[] = new StringBuffer[3];
//設定a1陣列的值
for (int i=0;i<a1.length;i++) {
a1[i] = new StringBuffer(Integer.toString(i));
}
//複製陣列
System.arraycopy(a1, 0, a2, 0, a1.length);
//顯示a1的內容
System.out.print(" 更改a2元素前陣列a1的元素: ");
for (int i=0;i<a1.length;i++) {
System.out.print(a1[i] + " ");
}
System.out.println();
//更改陣列a2第2個元素
a2[0].replace(0, 1, "9");
//顯示a1的內容
System.out.print(" 陣列a1的元素: ");
for (int i=0;i<a1.length;i++) {
System.out.print(a1[i] + " ");
}
//顯示a2的內容
System.out.print(" 陣列a2的元素 ");
for (int i=0;i<a2.length;i++) {
System.out.print(a2[i] + " ");
}
System.out.println("");
}
}
sample068 了解例外
1.在java中,程式執行期間發生的錯誤,稱為例外(exception)
2.會出現arrayindexoutofbounds exception的例外
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
//將值存放在超過陣列長度的位置
public class exception {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = new int[3];
System.out.println("將值存入陣列a第5個元素");
a[4] = 10;
System.out.print("陣列a第5個元素是 ");
System.out.println(a[4]);
}
}
sample069 例外處理
1.使用try/catch,可以讓程式在執行時捕捉發生的錯誤,稱為
,exception handling(例外處理)
基本語法:
try{
可能發生的動作;
}catch (例外類別 變數名) {
例外發生時要做的動作;
}
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class exceptionusetrycatch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
int a[] = new int[3];
System.out.println("將值存入陣列a第5個元素是");
a[4] = 10;
System.out.print("陣列a第5個元素是");
System.out.println(a[4]);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("無法將值指定給陣列");
}
}
}
sample070 發生例外也一定執行的動作
1.無論例外有沒有發生,都會執行的動作(finally區塊)
2.無論try區塊裡面有沒有例外發生,finally區塊最後都會執行.即使發生例外,而
沒有對應的catch區塊,finally區塊也會執行
3.加上finally的例外處理基本語法如下:
try{
可能發生例外的動作;
} catch (例外類別 變數名) {
例外發生時要做的動作;
} finally {
一定要執行的動作;
}
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class exceptionusefinally {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
int a[] = new int[3];
System.out.println("將值存入陣列a第5個元素");
a[4] = 10;
System.out.print("陣列a第5個元素是 ");
System.out.println(a[4]);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("無法將值指定給陣列");
}finally {
System.out.println("所有動作都完成了");
}
}
}
sample072 強制拋出例外
1.使用throw
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class exceptionusethrow {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
throw new IndexOutOfBoundsException();
}catch(IndexOutOfBoundsException e) {
System.out.print("捕捉到 " + e);
System.out.println(" 例外 ");
}
}
}
sample073 使用自己定義的例外
1.繼承Exception類別加以延伸,就可以建立自己定義的例外類別
2.不過這個好像失敗ㄝ
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class exceptionusemyself {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
throw new TestException();
} catch (TestException e) {
System.out.print(" 捕捉到 " + e);
System.out.println(" 例外 ");
}
}
}
class TestException extends Exception {
TestException() {
System.out.println("自定例外的建構子");
}
}