D16

1. 集合的嵌套遍歷

public static void main(String[] args) {
		// 創建大集合
		ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>();

		// 創建第一個班級的學生集合
		ArrayList<Student> firstArrayList = new ArrayList<Student>();
		// 創建學生
		Student s1 = new Student("唐僧", 30);
		Student s2 = new Student("孫悟空", 29);
		Student s3 = new Student("豬八戒", 28);
		Student s4 = new Student("沙僧", 27);
		Student s5 = new Student("白龍馬", 26);
		// 學生進班
		firstArrayList.add(s1);
		firstArrayList.add(s2);
		firstArrayList.add(s3);
		firstArrayList.add(s4);
		firstArrayList.add(s5);
		// 把第一個班級存儲到學生系統中
		bigArrayList.add(firstArrayList);

		// 創建第二個班級的學生集合
		ArrayList<Student> secondArrayList = new ArrayList<Student>();
		// 創建學生
		Student s11 = new Student("諸葛亮", 30);
		Student s22 = new Student("司馬懿", 28);
		Student s33 = new Student("周瑜", 26);
		// 學生進班
		secondArrayList.add(s11);
		secondArrayList.add(s22);
		secondArrayList.add(s33);
		// 把第二個班級存儲到學生系統中
		bigArrayList.add(secondArrayList);

		// 創建第三個班級的學生集合
		ArrayList<Student> thirdArrayList = new ArrayList<Student>();
		// 創建學生
		Student s111 = new Student("宋江", 40);
		Student s222 = new Student("吳用", 35);
		Student s333 = new Student("高俅", 30);
		Student s444 = new Student("李師師", 22);
		// 學生進班
		thirdArrayList.add(s111);
		thirdArrayList.add(s222);
		thirdArrayList.add(s333);
		thirdArrayList.add(s444);
		// 把第三個班級存儲到學生系統中
		bigArrayList.add(thirdArrayList);

		// 遍歷集合
		for (ArrayList<Student> array : bigArrayList) {
			for (Student s : array) {
				System.out.println(s.getName() + "---" + s.getAge());
			}
		}
	}
2.LinkedList的特有功能

 * A:添加功能
 * public void addFirst(Object e)
 * public void addLast(Object e)
 * B:獲取功能
 * public Object getFirst()
 * public Obejct getLast()
 * C:刪除功能
 * public Object removeFirst()
 * public Object removeLast()
 */

public static void main(String[] args) {
		// 創建集合對象
		LinkedList link = new LinkedList();

		// 添加元素
		link.add("hello");
		link.add("world");
		link.add("java");

		// public void addFirst(Object e)
		// link.addFirst("javaee");
		// public void addLast(Object e)
		// link.addLast("android");

		// public Object getFirst()
		// System.out.println("getFirst:" + link.getFirst());
		// public Obejct getLast()
		// System.out.println("getLast:" + link.getLast());

		// public Object removeFirst()
		System.out.println("removeFirst:" + link.removeFirst());
		// public Object removeLast()
		System.out.println("removeLast:" + link.removeLast());

		// 輸出對象名
		System.out.println("link:" + link);
	}

3.ArrayList去除集合中字符串的重複值(字符串的內容相同)

方式一:

public static void main(String[] args) {
		// 創建集合對象
		ArrayList array = new ArrayList();

		// 添加多個字符串元素(包含內容相同的)
		array.add("hello");
		array.add("world");
		array.add("java");
		array.add("world");
		array.add("java");
		array.add("world");
		array.add("world");
		array.add("world");
		array.add("world");
		array.add("java");
		array.add("world");

		// 創建新集合
		ArrayList newArray = new ArrayList();

		// 遍歷舊集合,獲取得到每一個元素
		Iterator it = array.iterator();
		while (it.hasNext()) {
			String s = (String) it.next();

			// 拿這個元素到新集合去找,看有沒有
			if (!newArray.contains(s)) {
				newArray.add(s);
			}
		}

		// 遍歷新集合
		for (int x = 0; x < newArray.size(); x++) {
			String s = (String) newArray.get(x);
			System.out.println(s);
		}
	}
方式二:

public static void main(String[] args) {
		// 創建集合對象
		ArrayList array = new ArrayList();

		// 添加多個字符串元素(包含內容相同的)
		array.add("hello");
		array.add("world");
		array.add("java");
		array.add("world");
		array.add("java");
		array.add("world");
		array.add("world");
		array.add("world");
		array.add("world");
		array.add("java");
		array.add("world");

		// 由選擇排序思想引入,我們就可以通過這種思想做這個題目
		// 拿0索引的依次和後面的比較,有就把後的幹掉
		// 同理,拿1索引...
		for (int x = 0; x < array.size() - 1; x++) {
			for (int y = x + 1; y < array.size(); y++) {
				if (array.get(x).equals(array.get(y))) {
					array.remove(y);
					y--;
				}
			}
		}

		// 遍歷集合
		Iterator it = array.iterator();
		while (it.hasNext()) {
			String s = (String) it.next();
			System.out.println(s);
		}
	}

4.去除集合中自定義對象的重複值(對象的成員變量值都相同)

	public static void main(String[] args) {
		// 創建集合對象
		ArrayList array = new ArrayList();

		// 創建學生對象
		Student s1 = new Student("林青霞", 27);
		Student s2 = new Student("林志玲", 40);
		Student s3 = new Student("鳳姐", 35);
		Student s4 = new Student("芙蓉姐姐", 18);
		Student s5 = new Student("翠花", 16);
		Student s6 = new Student("林青霞", 27);
		Student s7 = new Student("林青霞", 18);

		// 添加元素
		array.add(s1);
		array.add(s2);
		array.add(s3);
		array.add(s4);
		array.add(s5);
		array.add(s6);
		array.add(s7);

		// 創建新集合
		ArrayList newArray = new ArrayList();

		// 遍歷舊集合,獲取得到每一個元素
		Iterator it = array.iterator();
		while (it.hasNext()) {
			Student s = (Student) it.next();

			// 拿這個元素到新集合去找,看有沒有.contains()方法的底層依賴的是equals()方法。
 * 而我們的Student類中沒有equals()方法,這個時候,默認使用的是它父親Object的equals()方法
 * Object()的equals()默認比較的是地址值,所以,它們進去了。因爲new的東西,地址值都不同。
 * 按照我們自己的需求,比較成員變量的值,重寫equals()即可。
			if (!newArray.contains(s)) {//
				newArray.add(s);
			}
		}

		// 遍歷新集合
		for (int x = 0; x < newArray.size(); x++) {
			Student s = (Student) newArray.get(x);
			System.out.println(s.getName() + "---" + s.getAge());
		}
	}
5.請用LinkedList模擬棧數據結構的集合,並測試 ,的意思是:你自己的定義一個集合類,在這個集合類內部可以使用LinkedList模擬。

public class MyStack {
	private LinkedList link;

	public MyStack() {
		link = new LinkedList();
	}

	public void add(Object obj) {
		link.addFirst(obj);
	}

	public Object get() {
		// return link.getFirst();
		return link.removeFirst();
	}

	public boolean isEmpty() {
		return link.isEmpty();
	}
}
/*
 * MyStack的測試
 */
public class MyStackDemo {
	public static void main(String[] args) {
		// 創建集合對象
		MyStack ms = new MyStack();

		// 添加元素
		ms.add("hello");
		ms.add("world");
		ms.add("java");

		// System.out.println(ms.get());
		// System.out.println(ms.get());
		// System.out.println(ms.get());
		// NoSuchElementException
		// System.out.println(ms.get());
		
		while(!ms.isEmpty()){
			System.out.println(ms.get());
		}
	}
}

/*
 * 泛型類:把泛型定義在類上
 */
public class ObjectTool<T> {
	private T obj;

	public T getObj() {
		return obj;
	}

	public void setObj(T obj) {
		this.obj = obj;
	}
}
----------------------------------------------------

		ObjectTool<String> ot = new ObjectTool<String>();
		// ot.setObj(new Integer(27)); //這個時候編譯期間就過不去
		ot.setObj(new String("林青霞"));
		String s = ot.getObj();
		System.out.println("姓名是:" + s);

		ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
		// ot2.setObj(new String("風清揚"));//這個時候編譯期間就過不去
		ot2.setObj(new Integer(27));
		Integer i = ot2.getObj();
		System.out.println("年齡是:" + i);

6.泛型方法:把泛型定義在方法上

public class ObjectTool {
	public <T> void show(T t) {
		System.out.println(t);
	}
}
//泛型方法使用

ObjectTool ot = new ObjectTool();
		ot.show("hello");
		ot.show(100);
		ot.show(true)

/*
 * 泛型接口:把泛型定義在接口上
 */
public interface Inter<T> {
	public abstract void show(T t);
}
-------------------------------------------

//實現類在實現接口的時候
//第一種情況:已經知道該是什麼類型的了

//public class InterImpl implements Inter<String> {
//
//	@Override
//	public void show(String t) {
//		System.out.println(t);
//	}
// }

//第二種情況:還不知道是什麼類型的
public class InterImpl<T> implements Inter<T> {

	@Override
	public void show(T t) {
		System.out.println(t);
	}
}
--------------------------------------------------
public static void main(String[] args) {
		// 第一種情況的測試
		// Inter<String> i = new InterImpl();
		// i.show("hello");

		// // 第二種情況的測試
		Inter<String> i = new InterImpl<String>();
		i.show("hello");

		Inter<Integer> ii = new InterImpl<Integer>();
		ii.show(100);
	}

/*
 * 泛型高級(通配符)
 * ?:任意類型,如果沒有明確,那麼就是Object以及任意的Java類了
 * ? extends E:向下限定,E及其子類
 * ? super E:向上限定,E極其父類
 */
public class GenericDemo {
	public static void main(String[] args) {
		// 泛型如果明確的寫的時候,前後必須一致
		Collection<Object> c1 = new ArrayList<Object>();
		// Collection<Object> c2 = new ArrayList<Animal>();
		// Collection<Object> c3 = new ArrayList<Dog>();
		// Collection<Object> c4 = new ArrayList<Cat>();

		// ?表示任意的類型都是可以的
		Collection<?> c5 = new ArrayList<Object>();
		Collection<?> c6 = new ArrayList<Animal>();
		Collection<?> c7 = new ArrayList<Dog>();
		Collection<?> c8 = new ArrayList<Cat>();

		// ? extends E:向下限定,E及其子類
		// Collection<? extends Animal> c9 = new ArrayList<Object>();
		Collection<? extends Animal> c10 = new ArrayList<Animal>();
		Collection<? extends Animal> c11 = new ArrayList<Dog>();
		Collection<? extends Animal> c12 = new ArrayList<Cat>();

		// ? super E:向上限定,E極其父類
		Collection<? super Animal> c13 = new ArrayList<Object>();
		Collection<? super Animal> c14 = new ArrayList<Animal>();
		// Collection<? super Animal> c15 = new ArrayList<Dog>();
		// Collection<? super Animal> c16 = new ArrayList<Cat>();
	}
}

class Animal {
}

class Dog extends Animal {
}

class Cat extends Animal {
}
7.JDK5的新特性:自動拆裝箱,泛型,增強for,靜態導入,可變參數,枚舉

/*
 * 可變參數:定義方法的時候不知道該定義多少個參數
 * 格式:
 * 		修飾符 返回值類型 方法名(數據類型…  變量名){
 * 
 * 		}
 * 
 * 		注意:
 * 			這裏的變量其實是一個數組
 * 			如果一個方法有可變參數,並且有多個參數,那麼,可變參數肯定是最後一個
 */				
public class ArgsDemo {
	public static void main(String[] args) {
		// 2個數據求和
		int a = 10;
		int b = 20;
		int result = sum(a, b);
		System.out.println("result:" + result);

		// 3個數據的求和
		int c = 30;
		result = sum(a, b, c);
		System.out.println("result:" + result);

		// 4個數據的求和
		int d = 30;
		result = sum(a, b, c, d);
		System.out.println("result:" + result);

		// 需求:我要寫一個求和的功能,到底是幾個數據求和呢,我不太清楚,但是我知道在調用的時候我肯定就知道了
		// 爲了解決這個問題,Java就提供了一個東西:可變參數
		result = sum(a, b, c, d, 40);
		System.out.println("result:" + result);

		result = sum(a, b, c, d, 40, 50);
		System.out.println("result:" + result);
	}

	public static int sum(int... a) {
		// System.out.println(a);
		//return 0;

		int s = 0;
		
		for(int x : a){
			s +=x;
		}
		
		return s;
	}

	// public static int sum(int a, int b, int c, int d) {
	// return a + b + c + d;
	// }
	//
	// public static int sum(int a, int b, int c) {
	// return a + b + c;
	// }
	//
	// public static int sum(int a, int b) {
	// return a + b;
	// }
}
8..獲取10個1-20之間的隨機數,要求不能重複

public static void main(String[] args) {
		// 創建產生隨機數的對象
		Random r = new Random();

		// 創建一個存儲隨機數的集合。
		ArrayList<Integer> array = new ArrayList<Integer>();

		// 定義一個統計變量。從0開始。
		int count = 0;

		// 判斷統計遍歷是否小於10
		while (count < 10) {
			//先產生一個隨機數
			int number = r.nextInt(20) + 1;
			
			//判斷該隨機數在集合中是否存在。
			if(!array.contains(number)){
				//如果不存在:就添加,統計變量++。
				array.add(number);
				count++;
			}
		}
		
		//遍歷集合
		for(Integer i : array){
			System.out.println(i);
		}
	}
9.鍵盤錄入多個數據,以0結束,要求在控制檯輸出這多個數據中的最大值

public static void main(String[] args) {
		// 創建鍵盤錄入數據對象
		Scanner sc = new Scanner(System.in);

		// 鍵盤錄入多個數據,我們不知道多少個,所以用集合存儲
		ArrayList<Integer> array = new ArrayList<Integer>();

		// 以0結束,這個簡單,只要鍵盤錄入的數據是0,我就不繼續錄入數據了
		while (true) {
			System.out.println("請輸入數據:");
			int number = sc.nextInt();
			if (number != 0) {
				array.add(number);
			} else {
				break;
			}
		}

		// 把集合轉成數組
		// public <T> T[] toArray(T[] a)
		Integer[] i = new Integer[array.size()];
		// Integer[] ii = array.toArray(i);
		array.toArray(i);
		// System.out.println(i);
		// System.out.println(ii);

		// 對數組排序
		// public static void sort(Object[] a)
		Arrays.sort(i);

		// 獲取該數組中的最大索引的值
		System.out.println("數組是:" + arrayToString(i) + "最大值是:"
				+ i[i.length - 1]);
	}

	public static String arrayToString(Integer[] i) {
		StringBuilder sb = new StringBuilder();

		sb.append("[");
		for (int x = 0; x < i.length; x++) {
			if (x == i.length - 1) {
				sb.append(i[x]);
			} else {
				sb.append(i[x]).append(", ");
			}
		}
		sb.append("]");

		return sb.toString();
	}







發佈了3 篇原創文章 · 獲贊 23 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章