2011年5月31日 星期二

5/31筆記

sample067-1 複製陣列
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("自定例外的建構子");
  }
   }

















2011年5月30日 星期一

5/30筆記

sample063 取得陣列的長度
1.陣列名稱.length
2.二維陣列時,指定索引值,可以得到該索引的長度(列數)
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class arraylength {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//宣告一維陣列
int a[] = new int[3];
//取得一維陣列的長度
System.out.print("陣列a的長度是 ");
System.out.println(a.length);

//宣告二維陣列
int b[][] = new int[2][3];
//取得二維陣列的長度
System.out.print("陣列b的長度是(行數)是 ");
System.out.println(b.length);
System.out.print("陣列b的長度是(列數)是 ");
System.out.println(b[0].length);


}


}


sample064 設定陣列初始值
1.初始化一維陣列
   int a[ ] = {10, 20, 30}
2.初始化二維陣列
   int a[ ][ ] = {{10,20,30}, {40,50,60}}
   結果
   10 40
   20 50
   30 60
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class arrayinitial {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//初始化一維陣列
int a[] = {10,20,30};
System.out.println("顯示一維陣列的元素");
for(int i=0;i<a.length;i++) {
System.out.print("陣列第 "+ (i+1) + " 項的值是 ");
System.out.println(a[i]);
}

//初始化二維陣列
int b[][] = {{10,20,30},{40,50,60}};
System.out.println("顯示二維陣列的元素");
for(int i=0;i<b.length;i++) {
for(int j=0;j<b[0].length;j++) {
    System.out.print("陣列第 "+ (i+1) + " 行第 ");
    System.out.println((j+1) +" 列的值是 "+b[i][j]);
    
    }
             }
}
}


sample065 在陣列內搜尋
1.使用for迴圈進行尋序搜尋
2.arraysearch方法,找到想要找的值,傳回該值存放索引值+1的數
   ,如果找不到,則傳回-1
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class arraysearch {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a[] = {10,20,30,40,50};

//搜尋陣列中元素10的位置
System.out.print("10在陣列的第");
System.out.println(arraysearch(a,10) + "項");

//搜尋陣列中元素40的位置
System.out.print("40在陣列第");
System.out.println(arraysearch(a,40) + "項");
        
//搜尋陣列中元素60的位置
//因為找不到,故傳回-1
System.out.print("60在陣列的第");
System.out.println(arraysearch(a,60) + "項");
}


private static int arraysearch(int array[],int value) {
for (int i=0;i<array.length;i++) {
if (array[i] == value) {
  //傳回所在的位置
   return(i+1);
}
}

//找不到時,傳回-1
return(-1);
}
}




sample066 排序陣列的內容
1.氣泡排序(bubble sort):氣泡排序會不斷比較相鄰兩個
   元素的值,當大小關係相反時就互相交換,一直到沒有
   值需要交換為止.
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//氣泡排序
public class bubblesort {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a[] = {20,50,10,30,40};
System.out.println("排序前的陣列");
for (int i=0;i<a.length;i++) {
System.out.println(a[i]);
}

//將陣列遞增排序
sort(a);

System.out.println("排序後的陣列");
for (int i=0;i<a.length;i++) {
System.out.println(a[i]);

}



}
    public static void sort(int array[]) {
   
    int i = array.length;
    while (--i >= 0) {
     for (int j=0 ;j<i;j++) {
     if (array[j] > array[j+1]) {
      //交換元素值
     int tmp = array[j];
     array[j] = array[j+1];
     array[j+1] = tmp;
     }
     }
    }
    }
}


2.快速排列(quick sort):快速排列是先決定基準值,再以不斷分割資料
   達到排序陣列的效果,將其他元素與該基準值比較,分為較大和較小
   的兩個集合,並重複進行這些動作
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//quick sort
public class quicksort {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a[] = {20,50,10,30,40};
System.out.println("排序前的陣列");
for (int i=0;i<a.length;i++) {
    System.out.println(a[i]);
    }

//將陣列遞增排序
sort(a, 0, a.length-1);

System.out.println("排序後的陣列");
for (int i=0;i<a.length;i++) {
 System.out.println(a[i]);
}
}

static void sort(int array[], int start, int end) {

int low = start;
int high = end;
if (start >= end) {
 return;
}
int mid = array[(start+end)/2];
do {
while (array[low] > mid) {
low++;
}
while (array[high] < mid) {
high--;
}
if (low <= high) {
int temp = array[low];
array[low++] = array[high];
array[high--] = temp;
}
}while (low <= high);
sort(array,start,high);
sort(array,low,end);
}
}














2011年5月25日 星期三

5/26筆記

今天練習的部分分為兩大部分(1)陣列(2)列外處理

sample061 使用一維陣列
1.宣告陣列:int a[ ];
2.配置陣列的長度的記憶體空間:配置陣列元素空間時,[ ]裡設定的值
   就是陣列元素的數量(長度).如下列所示,請注意設定為3個元素時,索引
   值可用範圍是0~2
   a = new int[3];
3.將值指定給陣列元素,方法與變數相同
   a[0] = 1;
4.宣告陣列和配置記憶體空間也可以簡單寫成一行,
    int a[ ] = new int[3]
程式
/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class onearray {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//宣告陣列
int a[];
//配置容納陣列元素的記憶體空間

a = new int[3];
//指定陣列元素的值
a[0] = 10;
a[1] = 20;
a[2] = 30;
//顯示陣列元素的值
for(int i=0;i<3;i++) {
System.out.print(" 陣列第  " + (i+1) + " 項的值是 ");
System.out.println(a[i]);

}


}





sample062 使用二維陣列
1.宣告陣列:int a[ ][ ];
2.配置陣列的長度的記憶體空間
   a = new int[2][3]-配置了2*3=6個int型態元素的空間
3.將值指定給陣列元素
   a[1][2] = 1;前面是行,後面是列
4.宣告陣列和配置記憶體空間也可以簡單寫成一行
   int a[ ][ ] = new int[2][3];
5.對於行,列的問題一開始困惑我許久,所以這裡做個說明
  台灣用法:行=column=直  列=row=橫   但是
  大陸用法與台灣相反!! 行=row=橫  列=column=直
  相關網址:

  http://lifehaskilledme.blogspot.com/2007/08/blog-post.html
  http://caterpillar.onlyfun.net/Gossip/JavaGossip-V1/TwoDimensionArray.htm
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class twoarray {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//宣告變數
int a[][];
//配置元素空間
a = new int[2][3];
//指定陣列元素的值
a[0][0] = 10;
a[0][1] = 20;
a[0][2] = 30;
a[1][0] = 40;
a[1][1] = 50;
a[1][2] = 60;
//顯示陣列元素
for(int i=0;i<2;i++) {
for(int j=0;j<3;j++) {
System.out.print(" 陣列第 " + (i+1) + "行");
System.out.print(" 第 "+ (j+1) +" 列的值是 ");
System.out.println(a[i][j]);
}
}


}


}















5/25 筆記

sample051 使用stringbuffer字串
1.需要時常變更內容的字串
2.StringBuffer類別的效能是優於類別String
3.更完整的資料:http://mis.hwai.edu.tw/~kevin/MISProject/JAVAProject/chapter5/c5-2.htm
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class stringbuffer {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StringBuffer str = new StringBuffer("Hello World");   //宣告StringBuffer物件,並指定內容
 
        System.out.println("變更前,str的hash Code為:" + str.hashCode());
        System.out.println("變更前,str的內容為 ----- " + str);


        str.append(" Hello Java");              //現在更改str的內容
        System.out.println("變更後,str的hash Code為:" + str.hashCode());
        System.out.println("變更後,str的內容為 ----- " + str);


        String newStr = new String(str);
        System.out.println("新建立的String物件,內容為:" + newStr);
        System.out.println("新建立的String物件,hash Code為:" + newStr.hashCode());
}


}


sample052 修改stringbuffer字串
1.使用stringbuffer類別的charAt方法,可以取得字串中的一個字元
2.setCharAt,可以更改字串中的字元
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class charatsetcharat {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StringBuffer sb = new StringBuffer("Hello Java StringBuffer!");

System.out.println(sb);

//取得更改前的字元
System.out.println("更改前的字元:" + sb.charAt(4));

//更改字元,char使用單引號
sb.setCharAt(4,'j');

System.out.println(sb);


}


}


sample053 取得,更改stringbuffer字串的長度
1.使用stringbuffer類別的length方法,傳回字串緩衝區長度
2.使用stringbuffer類別的setlength方法,更改字串緩衝區的長度,當延長字串時
   會在原來後方加上null字元,當要縮短字串時,則會刪除多出來的部分
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class lengthsetlength {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StringBuffer sb = new StringBuffer("Hello Java StringBuffer!");
int len = sb.length();
System.out.print(sb + " 的長度是  ");
System.out.println(len + " 個字元 ");
sb.setLength(len - 7);
System.out.println(" 刪除後方7個字: " + sb);
//比原來少7個,再加上原來7個,所以總共出現14個null
sb.setLength(len + 7);
System.out.println(" 增加後方14個null字: " + sb);



}


}


sample054 取得stringbuffer字串佔用的記憶體空間
1.使用stringbuffer類別的capacity方法,可以取得字串佔用的記憶體空間
2.java為了降低伸長字串時重新分配記憶體所耗費的時間,所以多配置了16個字元
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class capacity {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StringBuffer sb = new StringBuffer("Hello Java StringBuffer!");
int len = sb.length();
System.out.print(sb + " 的長度是 ");
System.out.println(len + " 個字元 ");
int cap = sb.capacity();
System.out.println(sb + " 佔用的空間是 " + cap);



}


}


sample055 在stringbuffer字串後方加入文字
1.使用stringbuffer類別的append方法,可以在字串後方加入文字
2.append方法除了可以加入string類別的物件以外,還有多載用來加入
   boolean  char   double  float  int  long等型態參數的方法,這些型態的值
   會以字串的型式加入在字串後方
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class append {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StringBuffer sb = new StringBuffer("Hello Java");
sb.append("StringBuffer!");
System.out.println(sb);
}


}


sample056 在stringbuffer字串中插入文字
1.使用stringbuffer類別的insert方法,可以在字串後方插入另一個字串
2.字串緩衝區會增加插入字串的長度
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class insert {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StringBuffer sb = new StringBuffer("Hello  StringBuffer!");
sb.insert(6,"Java");
System.out.println(sb);


}


}


sample057 在stringbuffer字串中刪除文字
1.使用stringbuffer類別的delete方法,可以刪除字串中的文字
2.StringBuffer delete(int start,int end)-起點start而終點則是end-1
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class delete {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StringBuffer sb = new StringBuffer("Hello Java StringBuffer!");
sb.delete(6,10);
System.out.println(sb);



}


}


sample058 在stringbuffer字串中取代文字
1.使用stringbuffer類別的replace方法,
2.StringBuffer replace(int start,int end,String str)-起點是start-終點end-1
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class replace {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

StringBuffer sb = new StringBuffer("Hello Java StringBuffer!");
sb.replace(7,10,"AVA");
System.out.println(sb);


}


}


sample059 選擇string和stringbuffer的區別


要使用string還是stringbuffer類別,有兩大指標
1.內容若為固定不變的字串,則使用string物件
2.如果字串有改變內容或大小的可能性,就使用stringbuffer物件


sample060 string和stringbuffer的轉換
1.從string類別轉換為stringbuffer類別,使用new
2.從stringbuffer類別轉換為string類別,使用tostring
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class transferstringandstringbuffer {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//建立String類別的字串
String s1 = "轉換成StringBuffer";
//String類別到stringbuffer類別的轉換
StringBuffer sb1 = new StringBuffer(s1);
System.out.println(sb1);

//建立StringBuffer類別的字串
StringBuffer sb2 = new StringBuffer("轉換成String");
//StringBuffer類別到String的轉換
String s2 = sb2.toString();
System.out.println(s2);



}


}