2020/12/18java作業十一

巴拉拉小摩托發表於2020-12-22

 

 

8.如何實現集合物件排序?定義一個複數類並按照複數實部大小對複數物件進行排序。

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

public class complex {
        public int real;
        public int virtual;
        public complex(int real,int virtual) {
        	this.real=real;
        	this.virtual=virtual;
        }
        
        public String toString() {
        	return String.valueOf(real)+" +"+String.valueOf(virtual)+"i";
        }
        public static void main(String[] args) {
        	ArrayList<complex> list=new ArrayList<complex>();
        	complex a=new complex(3,4);
        	complex b=new complex(-1, 5);
        	complex c=new complex(2, 1);
        	list.add(a);list.add(b);list.add(c);
        	System.out.println(list);
        	Collections.sort(list, new Comparator<complex>() {
        		public int compare(complex c1,complex c2) {
        			return c1.real-c2.real;
        		}
			});
        	System.out.println(list);
        }
}

10.對第七章第6題進行適當改造,將異常型別與中文提示儲存在一種 集合類當中,從而實現相應的功能。

import java.util.HashMap;

class myException{
    @SuppressWarnings("rawtypes")
    public HashMap<Class, String> hm = new HashMap<>();
    public myException() {
        hm.put(NullPointerException.class, "空指標異常");
        hm.put(ArithmeticException.class, "算術異常");
        hm.put(Exception.class, "其他異常");
    }
}
public class test {

    public static void main(String[] args) {
        myException ex=  new myException();
        try {
        	String s = null;
            //System.out.println(1/0);//除零異常
            System.out.println(s.charAt(0));//空指標異常
        }catch (NullPointerException e) {
        	Class<?> c = NullPointerException.class;
            System.out.println(ex.hm.get(c));
        }catch (ArithmeticException e) {
        	Class<?> c = ArithmeticException.class;
            System.out.println(ex.hm.get(c));
        }catch (Exception e) {
        	Class<?> c = Exception.class;
            System.out.println(ex.hm.get(c));
            e.printStackTrace();
        }
    }
}

1.

Arraylist

import java.util.ArrayList;
import java.util.Iterator;
public class test {
	public static void main(String[] args) {
		ArrayList<Integer> li = new ArrayList<Integer>();
		for( int i=0;i<10;i++ ) //新增元素
		li.add(i); 

		//遍歷元素
		for (int i = 0; i < li.size(); i++) {
			int n = li.get(i);
			System.out.print(n+" ");
		}
		System.out.println(" ");
		li.add(0,99);//在第一個位置新增
		li.remove(1);//刪除元素
		li.remove((Integer)5);//移除遇到的第一個指定個元素
		//迭代器
		Iterator<Integer> it = li.iterator();
		while (it.hasNext()) {
			int n = it.next();
			System.out.print(n+" ");
		}
	}
}

LinkedList

import java.util.Iterator;
import java.util.LinkedList;

public class test {
	public static void main(String[] args) {
		String String = new String("AAA");
		String String2 = new String("BBB");
		String String3 = new String("CCC");
		//建立LinkedList集合
		LinkedList<String> li = new LinkedList<String>();
		li.add(String);
		li.add(String2);
		li.add(String3);
		//遍歷
		for (int i = 0; i < li.size(); i++) {
			String n = li.get(i);
			System.out.println(n);
		}
		System.out.println(" ");
		li.add(0,String3);//在第一個位置新增
		li.remove(1);//移除第二個元素
		li.remove("CCC");//移除遇到的第一個指定個元素
		//迭代器
		Iterator<java.lang.String> it = li.iterator();
		while (it.hasNext()) {
			String n = (java.lang.String) it.next();
			System.out.println(n);
		}
	}
}

HashSet

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

public class test {   
    public static void main(String[] args) {   
        HashSet<Character> set = new HashSet<Character>();
        set.add('a');   
        set.add('b');   
        set.add('c');   
        set.add('d');   
        set.add('e');   
        //迭代遍歷:
        Iterator<Character> it = set.iterator();
        while (it.hasNext()) {
          char str = it.next();
          System.out.print(str+" ");
        }
        System.out.println();
        set.remove('c');//去掉指定元素
        
        //for迴圈遍歷:
        for (char str : set) {
              System.out.print(str+" ");
        }
        System.out.println(set.size());//輸出大小
    }   
}

HashMap

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class test {
	public static void main(String[] args) {

		HashMap<Integer, String> hashMap = new HashMap<>();
		// 新增元素
		for (int i = 0; i < 5; i++) {
			hashMap.put(i, "隨機數" + (int) (Math.random() * 100));
		}
		// 獲取元素數量
		System.out.println("size: "+hashMap.size());
		// map的遍歷
		for(Map.Entry<Integer, String> entry :hashMap.entrySet() ) {
			System.out.println(entry.getKey()+"  "+entry.getValue());
		}
		// 獲取key是3的元素
		System.out.println("key是3: "+hashMap.get(2));
		
		//遍歷所有的key
		for(int key:hashMap.keySet()) {
			System.out.print(key+" ");
		}
		//遍歷所有的value
		for(String value:hashMap.values()) {
			System.out.print(value+" ");
		}
		System.out.println();
		System.out.println("包含鍵值為4的嗎? "+hashMap.containsKey(4));
		System.out.println("包含指定值為100的嗎? "+hashMap.containsValue("隨機數100"));
		//迭代器遍歷
		Iterator<Entry<Integer, String>> it = hashMap.entrySet().iterator();
		while(it.hasNext()) {
			Entry<Integer, String> next = it.next();
			System.out.println(next.getKey()+"  "+next.getValue());
		}
		System.out.println("-----------------");
		hashMap.remove(3);//移除鍵值為3的元素
		//通過鍵找值遍歷
		for(int k: hashMap.keySet()) {
			System.out.println(k+"  "+hashMap.get(k));
		}
	}
}