1. 程式人生 > >初學java之util

初學java之util

List:

//不能排序,可以有重複。
import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static List <Integer> list = new LinkedList<Integer>();
	public static void main(String[] args){
		int n = cin.nextInt();
		while(n-->0) {
			list.add(cin.nextInt());//輸入
		}
		System.out.println(list.get(3));
		list.remove(2);
//		 forEach 迴圈
		for (int x:list) {
			System.out.print(x+" ");
		}
		System.out.println();
		for (int i = 0; i < list.size();i++) {
			System.out.print(list.get(i)+" ");
		}
		System.out.println();
/*
 * 測試樣例
5
2 3 5 0 1
0
2 3 0 1 
2 3 0 1
*/
	}
}

Set:

//TreeSet 是有序  從小到大   唯一但是HashSet我還不確定
import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static Set<String> set1 = new TreeSet<String>();
	static Set<String> set2 = new HashSet<String>();
	public static void main(String[] args){
		int n = cin.nextInt();
		for (int i = 0; i < n;i++) {
			set1.add(cin.next());//輸入
		}
//		for (int i = 0; i < n;i++) {
//			set2.add(cin.next());
//		}
		for (String x:set1) {
			System.out.print(x+" ");
		}
//		for (String x:set2) {
//			System.out.println(x+" ");
//		}
		System.out.println();
	}
/*
5
c d a b a
a b c d 
*/
}

Map<key-value> :

// Map<key-value>
//TreeMap 按照鍵值從小到大自動排序  HashMap 還有點迷
import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static Map<String,Integer> map = new TreeMap<String,Integer>();
	static Map<String,Integer> map1 = new HashMap<String,Integer>();
	public static void main(String[] args){
		int n = cin.nextInt();
		while(n-->0) {
			map.put(cin.next(), cin.nextInt());
			//get()通過鍵來獲取對應的值
		}
//			System.out.println(map.get("xx"));
////			遍歷map
////		1.forEach
//			Set<Map.Entry<String, Integer> >set = map.entrySet();
//			for (Map.Entry<String, Integer> e:set) {
//				System.out.println(e.getKey()+" "+e.getValue());
//			}
			/*
			 3
             xx 1
             cc 2
			 aa 3
             1   //System.out.println(map.get("xx"));
             aa 3
             cc 2
             xx 1
             */
			//2.iterator 通過迭代器遍歷集合
//			Collection<Integer> c = map.values();
//			Iterator<Integer> i = c.iterator();
//			while(i.hasNext()) {
//				System.out.println(i.next());
//			}
			/*
			 * 測試樣例
			 3
             c 1
             b 3
             a 4
             4
             3
             1
             */
//			//3.推薦 利用keyset 集合遍歷map
//			Set<String> key = map.keySet();
//			for(String k:key) {
//				System.out.println(k+": "+map.get(k));
//			}
			/*
			 3
             xx 1
             cc 2
             aa 3
             aa: 3
             cc: 2
             xx: 1
             */
////			//4.keyset iterator
//		    Set<String> key = map.keySet();
//			Iterator <String> it = key.iterator();
//			while(it.hasNext()) {
//				String s = it.next();//我覺得這樣寫也行
//				//學長這樣寫的String s = (String) it.next();
//				System.out.println(s+": " + map.get(s));
//			}
			/*
			 3
            xx 1
            cc 2
            aa 3
            aa: 3
            cc: 2
            xx: 1
            */
	}
}

Queue 先進先出 PriorityQueue按優先順序自動排列(從小到大)

import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static 	PriorityQueue<Integer> qq = new PriorityQueue<Integer>();//優先佇列
	static 	Queue<Integer> q = new LinkedList<Integer>();//普通佇列,先進先出
	public static void main(String[] args){
		int n = cin.nextInt();
		int x;
		while(n-->0) {
			x=cin.nextInt();
			q.add(x);
		}
		//forEach 遍歷集合
		for(int e:q) {
			System.out.print(e+" ");
		}
		System.out.println();
		//size() 返回佇列中的元素個數
		System.out.println(q.size());
		//peek() 返回隊首元素
		System.out.println(q.peek());
		//poll 移除佇列首元素,同時返回首元素的值
		System.out.println(q.poll());
		while(!q.isEmpty()) {
			System.out.print(q.poll()+" ");
		}
		System.out.println();
		System.out.println(q.size());
	}
/*
 * 普通佇列測試樣例
5
3 4 6 5 2
3 4 6 5 2 
5
3
3
4 6 5 2 
0
*/
//		int n = cin.nextInt();
//		int x;
//		while(n-->0) {
//			x=cin.nextInt();
//			qq.add(x);
//		}
//		//forEach 遍歷集合
//		for(int e:qq) {
//			System.out.print(e+" ");//不按照從小到大輸出
//		}
//		System.out.println();
//		//size() 返回佇列中的元素個數
//		System.out.println(qq.size());
//		//peek() 返回隊首元素
//		System.out.println(qq.peek());
//		//poll 移除佇列首元素,同時返回首元素的值
//		System.out.println(qq.poll());
//		while(!qq.isEmpty()) {
//			System.out.print(qq.poll()+" ");//按照從小到大輸出
//		}
//		System.out.println();
//		System.out.println(qq.size());
//	}
/*
 * 優先佇列測試樣例
5
3 4 6 5 2
2 3 6 5 4 
5
2
2
3 4 5 6 
0
*/
}

Stack: 後進先出

import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static 	Stack<Integer> s = new Stack<Integer>();
	public static void main(String[] args){
		int n = cin.nextInt();
		while(n-->0) {
			int x = cin.nextInt();
			s.push(x);
		}
		//輸出棧內元素個數
		System.out.println(s.size());
		//輸出棧頂元素
		System.out.println(s.peek());
		//彈出棧頂元素,也就是刪除棧頂元素,並且返回棧頂元素的值
		System.out.println(s.pop());
		//輸出座標為2的元素,座標從0開始
		System.out.println(s.get(2));
		while(!s.isEmpty()){
			System.out.print(s.pop()+" ");
		}
		System.out.println();
		System.out.println(s.size());
	}
/*
5
3 4 2 1 5
5
5
5
2
1 2 4 3 
0
*/
}

Vector:

import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static 	Vector<Integer> v = new Vector<Integer>();
	public static void main(String[] args){
		int n=cin.nextInt();
		while(n-->0) {
			int x=cin.nextInt();
			v.add(x);//輸入
		}
		for(int i = 0; i < v.size();i++) {
			System.out.print(v.get(i)+" ");
		}
		System.out.println();
		System.out.println(v.remove(2));//移除下標為2的元素,下標從0開始, 並且返回該元素
		for(int i = 0; i < v.size();i++) {
			System.out.print(v.get(i)+" ");
		}
		System.out.println();
		while(!v.isEmpty()){
			System.out.print(v.remove(0)+" ");
		}
		System.out.println();
		System.out.println(v.size());//輸出元素個數
	}
	/*
	 *測試樣例
5
3 1 4 5 2
3 1 4 5 2 
4
3 1 5 2 
3 1 5 2 
0
*/
}