顯示具有 JAVA物件導向實作 標籤的文章。 顯示所有文章
顯示具有 JAVA物件導向實作 標籤的文章。 顯示所有文章

2013年9月11日 星期三

泛型集合類別-ListIterator介面輸出元素

import java.util.ArrayList;
import java.util.ListIterator;

public class ch10_6_4_listiterator {

    public static void main(String[] args) {
        // TODO 自動產生的方法 Stub
        ArrayList<String> alist = new ArrayList<String>();
        alist.add("台北");
        alist.add("林口");
        alist.add("桃園");
        alist.add("基隆");
        System.out.println("ArrayList的元素為: " + alist);
        ListIterator<String> lterator = alist.listIterator(0);
        System.out.print("ListIterator的元素為: ");
        while (lterator.hasNext()) {
            System.out.print(lterator.next() + "  ");
        }
        ListIterator<String> literator1 = alist.listIterator(alist.size());
        System.out.println("");
        System.out.print("ListIterator的元素為(反向): ");
        while(literator1.hasPrevious()){
            System.out.print(literator1.previous()+ "  ");
        }
    }

}


實作List介面的集合物件除了可以使用Iterator介面外,還可以使用ListIterator介面,這是Iterator介面的子介面。ListIterator介面除了使用一致走訪方法外,還可以雙向走訪集合物件的元素,即從頭到尾,或從尾到頭來走訪元素。

2013年9月10日 星期二

泛型集合類別-Iterator介面輸出元素

import java.util.HashSet;
import java.util.Iterator;

public class ch10_6_3 {

    public static void main(String[] args) {
        // TODO 自動產生的方法 Stub
HashSet<String> hset=new HashSet<String>();
hset.add("三芝");
hset.add("淡水");
hset.add("金山");
System.out.println("Hashset方法呼叫內容為:"+hset);
Iterator<String> iterator=hset.iterator();
System.out.print("Iteretor介面呼叫內容為:");
while(iterator.hasNext()){
    System.out.print(iterator.next()+"  ");
}
    }

}


使用Collection、Set和List介面的iterator()方法,就可以取得Iterator<E>介面物件。
例如:HashSet<String>物件hset可以使用上表方法取得Iterator介面物件,如下所示:

Iterator<String> iterator = hset.iterator();

Android App實作:ListView +button

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
    ListView LV01;
    Button BT01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //連結
        LV01=(ListView) this.findViewById(R.id.LV01);
        BT01=(Button) this.findViewById(R.id.BT01);
        //定義事件
        BT01.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO 自動產生的方法 Stub
                ArrayList<String> alist=new ArrayList<String>();
                alist.add("小白");
                alist.add("小叮噹");
                alist.add("小叮鈴");
                alist.add("小新");
                ArrayAdapter <String> Aadapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, alist);
                LV01.setAdapter(Aadapter);
            }
           
           
           
           
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

泛型集合類別-ArrayList類別

import java.util.ArrayList;

public class ch10_6_2_arraylist {

    public static void main(String[] args) {
        // TODO 自動產生的方法 Stub
        ArrayList<String> alist = new ArrayList<String>(4);
        String name = "蠟筆小新";
        alist.add("小丸子");
        alist.add("蠟筆小新");
        alist.add("小甜甜");
        alist.add("小叮噹");
        System.out.println("物件尺寸:" + alist.size());
        alist.add(2, name);//新增元素
        System.out.println("物件尺寸:" + alist.size());
        System.out.println("集合物件(1):");
        for (int i = 0; i < alist.size(); i++) { //利用迴圈取得元素
            System.out.print(i + ")" + alist.get(i) + " ");
        }
        System.out.println(" ");
        System.out.println("集合物件(2):");
        for (String element : alist) {
            System.out.print(element + " ");
        }
        System.out.println(" ");
        System.out.println("搜尋:"+name);
        System.out.println("indexOf():"+alist.indexOf(name));
        System.out.println("lastindexOf():"+alist.lastIndexOf(name));
        alist.set(3,"小叮鈴");//取代元素
        System.out.println("取代元素三:"+alist);
        alist.remove(0);//刪除元素
        System.out.println("取代元素0:"+alist);
    }

}

ArrayList類別實作List介面,使用類似陣列方式來儲存,元素是使用索引位置來依序的存入,我們只需將元素新增或插入ArrayList物件,並不用事先宣告物件尺寸,如同一種可自動調整陣列尺寸的動態陣列

泛型集合類別-HashSet類別

import java.util.HashSet;

public class 集合物件 {

    public static void main(String[] args) {
        // TODO 自動產生的方法 Stub
        HashSet<String> hset = new HashSet<>();
        System.out.println("集合物件是否為空:" + hset.isEmpty());
        String name1 = "林至玲";
        String name2 = "王金評";
        hset.add("胖胖褲豬");
        hset.add(name1);
        hset.add(name2);
        hset.add("多拉A夢");
        System.out.println("集合物件的尺寸為: "+hset.size());
        System.out.println("集合物件是否為空:" + hset.isEmpty());
        System.out.println("集合物件有林至玲:" + hset.contains(name1));
        hset.remove(name1);
        System.out.println("集合物件有林至玲:" + hset.contains(name1));
        System.out.println("集合物件內容為:" +hset);
        hset.clear();
    }

}

程式碼在HashSet類別之後,使用「<」和「>」括起的資料型態是泛型型態,可以指定集合物件儲存元素的資料型態,以便Java編譯程式自行追蹤記錄元素的資料型態,所以取出集合物件的元素時,就不需使用程式碼來執行型態轉換。 

HashSet類別實作Set介面,繼承Collection介面的方法且使用「雜湊表」(Hash Table)演算法來改進新增、刪除和存取集合物件元素的執行效率,其儲存元素的排列和插入順序不同,也不保證擁有固定的排列順序 

2013年9月9日 星期一

JAVA-方法丟出例外

Java程式是使用throw指令來自行丟出例外。

public class ch10_2_2 {

    public static void main(String[] args) {
        // TODO 自動產生的方法 Stub
        int result;
        try {
            int a = (int) (Math.random() * 10);
            int b = (int) (Math.random() * 10);
            int c = (int) (Math.random() * 10);
            result = cal(a, b, c);
            System.out.println("計算結果為: " + result);
        } catch (IllegalArgumentException e) {
            // TODO 自動產生的 catch 區塊
            System.out.println("例外說明: "+e.getMessage());
            System.out.println("例外原因: ");
            e.printStackTrace();
        } catch (ArrayIndexOutOfBoundsException e) {
            // TODO 自動產生的 catch 區塊
            System.out.println("例外說明: "+e.getMessage());
            System.out.println("例外原因: ");
            e.printStackTrace();
        }
    }

    static int cal(int a, int b, int c) throws IllegalArgumentException, ArrayIndexOutOfBoundsException{
        int index;
        int[] data = { 22, 14, 36, 68, 87 };
        if (c <= 0) {
            throw new IllegalArgumentException("C小於等於0。");
        } else {
            index = a * b / c;
            if (index >= 5) {
                throw new ArrayIndexOutOfBoundsException("索引值大於5。");
            }
        }
        return data[index];
    }
}

Java例外處理程序 、finally、try/catch

例外處理程序


public class ch10_1_1 {

    public static void main(String[] args) {
        // TODO 自動產生的方法 Stub
        int i;
        try {
            for (i = 2; i > -1; i--) {
                System.out.println("計算結果為: " + 10 / i);
            }
        } catch (ArithmeticException e) {
            // TODO 自動產生的 catch 區塊
            System.out.println("例外說明: "+e.getMessage());
            System.out.println("例外原因: ");
            e.printStackTrace();
        }
        finally{
            System.out.println("例外處理結束");
        }
        System.out.println("JAVA程式執行結束!!");
    }

}