Java入門學習筆記(十)——泛型和反射、常用泛型類、ArryList類、LinkedList類、Stack類、HashSet類、TreeSet類

2020-10-05 11:01:06

一、常用的泛型類

1.ArrayList類

package try_a_package;

import java.util.ArrayList;

class ArrayListTest {
	public static void main(String[] args)
	{
		ArrayList list1=new ArrayList();
		list1.add("我是一個");
		list1.add("憨憨");
		list1.add(1);
		boolean flag1=list1.contains("憨憨");
		if(flag1)
			System.out.println("我是憨憨");
		else
			System.out.println("我才不是憨憨呢!");
		System.out.println(list1.get(0));
		System.out.println(list1.size());
		System.out.println(list1.set(0, "我是兩個"));
		for(Object a:list1)
		{
			System.out.println(a);
		}
		// 這裡像刪除的元素,而不少索引,所以增加了Object的型別轉換來實現這個操作。
		list1.remove((Object)1);
		// 如果不加Object的話,會直接移除索引處的元素
		// 如果索引處的元素,不存在,如下面這個刪除Object後的情形
		// 則會丟擲越界的異常
		list1.remove((Object)2);
		for(int i=0;i<list1.size();++i)
		{
			Object o=list1.get(i);
			System.out.println(o);
		}
		System.out.println(list1.isEmpty());
		System.out.println(list1.indexOf("憨憨"));
		list1.clear();
		System.out.println(list1.isEmpty());
	}
}

2.LinkedList類

package try_a_package;

import java.util.LinkedList;

public class LinkedListTest {
	public static void main(String[] args) {
		// 構造
		LinkedList list = new LinkedList();
		// 插入元素
		list.add(4);
		list.add("String");
		list.add(1, 5);
		list.addFirst(1);
		list.addLast(8);
		// 讀取指定元素
		System.out.println(list.getFirst());
		System.out.println(list.getLast());
		System.out.println(list.get(2));
		int temp = list.indexOf(5);
		System.out.println(temp);
		temp = list.lastIndexOf(5);
		System.out.println(temp);
		// 注意,要進行強制型別
		temp = (int) list.peek();
		System.out.println(temp);
		// 移除元素
		temp = (int) list.poll();
		System.out.println(temp);
		temp = (int) list.remove();
		System.out.println(temp);
		temp = (int) list.get(0);
		System.out.println(temp);
		String str = (String) list.get(1);
		System.out.println(str);
		str = (String) list.remove(1);
		System.out.println(str);
		boolean flag = list.remove((Object) 5);
		System.out.println(flag);
		flag = list.remove((Object) 5);
		System.out.println(flag);
		temp = (int) list.removeFirst();
		System.out.println(temp);
		if (!list.isEmpty())
			temp = (int) list.removeLast();
		System.out.println(temp);
	}
}

3.Stack類

package try_a_package;

import java.util.Stack;
import java.util.Scanner;

public class StackTest {
	public static void main(String[] args)
	{
		Stack stack=new Stack();
		Scanner sc=new Scanner(System.in,"gbk");
		stack.push(1);
		stack.push(2);
		stack.push(5);
		while(!stack.isEmpty())
		{
			System.out.println(stack.pop());
		}
		sc.close();
	}
}

4.HashSet類

package try_a_package;

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

public class HashSetTest {
	// 輸出集合中的元素
	public static void print(HashSet a)
	{
		System.out.print("{");
		Iterator it=a.iterator();
		int c=0;
		while(it.hasNext())
		{
			int t=(int)it.next();
			if(c==0)
			{
				System.out.print(t);
			}
			else
			{
				System.out.print(","+t);
			}
			c++;
		}
		System.out.println("}");
	}
	// 合併兩個集合
	public static HashSet interSection(HashSet a,HashSet b)
	{
		HashSet c=new HashSet();
		Iterator it=a.iterator();
		while(it.hasNext())
		{
			int t=(int)it.next();
			if(b.contains(t))
			{
				c.add(t);
			}
		}
		return c;
	}
	// 求並集
	public static HashSet unionSection(HashSet a,HashSet b)
	{
		HashSet c=new HashSet(a);
		c.addAll(b);
		return c;
	}
	// 隨機生成元素,構成集合
	public static HashSet getSet(int num,int n)
	{
		HashSet h=new HashSet();
		Random rd=new Random();
		for(int i=0;i<n;++i)
		{
			int now_rd=rd.nextInt(num);
			h.add(now_rd);
		}
		return h;
	}
	// 主函數
	public static void main(String[] args)
	{
		HashSet a,b,c,d;
		a=getSet(10,6);
		b=getSet(10,6);
		print(a);
		print(b);
		c=interSection(a,b);
		d=unionSection(a,b);
		print(c);
		print(d);
	}
}

在這裡插入圖片描述

5.TreeSet

package try_a_package;

import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;

public class TreeSetTest {
	// 輸出集合中的所有元素(適用於List,Queue,Set)
	public static void printSet(Collection a)
	{
		System.out.print("(");
		Iterator it=a.iterator();
		for(int i=0;it.hasNext();++i)
		{
			int t=(int)it.next();
			if(i==0)
			{
				System.out.print(t);
			}
			else
			{
				System.out.print(","+t);
			}
		}
		System.out.println("}");
	}
	// 輸出ENode構成的集合中的元素
	public static void print(TreeSet<ENode> ts)
	{
		System.out.println("輸出集合中的元素:");
		System.out.println("{");
		Iterator it=ts.iterator();
		while(it.hasNext())
		{
			ENode node=(ENode)it.next();
			System.out.println(node);
		}
		System.out.println("}\n");
	}
	// 隨機產生由n個元素構成的集合
	public static TreeSet getTreeSet(int num,int n)
	{
		TreeSet h=new TreeSet();
		Random rd=new Random();
		for(int i=0;i<n;++i)
		{
			int tmp=rd.nextInt(num);
			h.add(tmp);
		}
		return h;
	}
	
	public static TreeSet<ENode> getDecare(TreeSet a,TreeSet b)
	{
		TreeSet<ENode> ts=new TreeSet<>();
		Iterator ita;
		ita=a.iterator();
		while(ita.hasNext())
		{
			int tmpa=(int)ita.next();
			Iterator itb=b.iterator();
			while(itb.hasNext())
			{
				int tmpb=(int) itb.next();
				ENode node=new ENode(tmpa,tmpb);
				ts.add(node);
			}
		}
		return ts;
	}
	
	// 主函數
	public static void main(String[] args)
	{
		TreeSet a,b;
		a=getTreeSet(10,3);
		b=getTreeSet(10,2);
		printSet(a);
		printSet(b);
		TreeSet<ENode> c=getDecare(a,b);
		print(c);
	}
}

在這裡插入圖片描述