201521123002《Java程序設計》第8周學習總結


1. 本周學習總結

1.1 以你喜歡的方式(思維導圖或其他)歸納總結集合與泛型相關內容。

2. 書面作業

本次作業題集集合

1.List中指定元素的刪除(題目4-1)
1.1 實驗總結

1.提交函數實驗題時只要提交編寫的函數即可,不然會出現編譯錯誤
2.出現了一次答案錯誤,點擊查看當時的代碼,發現錯誤點為誤用"=="比較字符串。

2.統計文字中的單詞數量並按出現次數排序(題目5-3)
2.1 偽代碼(簡單寫出大體步驟)

while(has next word)
 	if(the map doesn't contains the word)
 		put the word into the map
 	else
 		get the value in the map to add 1
sort the map
printout the result 

1.用HashMap先建立一個映射關系,

關鍵處

            if(cnt==null){
                map.put(key, 1);
            }else{
                map.put(key, cnt+1);
            }

2.然后轉化為集合用sort排序編寫compare

關鍵處

public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                // TODO Auto-generated method stub
                int x=o2.getValue()-o1.getValue();
                if(x!=0)
                    return x;
                else
                    return o1.getKey().compareTo(o2.getKey());
            }

3.最后輸出

2.2 實驗總結

1.可以看到出現了編譯錯誤用來是pta識別不了jdk8的語法,所以之后提交的時候不能用jdk8的語法。

2.在控制台輸入的時候不能用line

3.倒排索引(題目5-4)
3.1 截圖你的提交結果(出現學號)

3.2 偽代碼(簡單寫出大體步驟)
1.建索引 Map<String,Set<Integer>> map=new TreeMap<String,Set<Integer>>();
2.分為只有一個單詞和多個單詞來查找

create a map to keep the reference
printout the reference
search the keywords
create an Collection to keep the result
if(the Collection  is empty)
	output "found 0 results"
else 
	output Collection

3.3 實驗總結

這題還是比較難的,用到了很多的種集合類型,做完這題收獲還是比較大的,學會用s.split(" "),但是我對自己查找單詞的算法不是很滿意,感覺有點冗長。算法啊算法。

4.Stream與Lambda
編寫一個Student類,屬性為:

private Long id;
private String name;
private int age;
private Gender gender;//枚舉類型
private boolean joinsACM; //是否參加過ACM比賽

創建一集合對象,如List,內有若干Student對象用於后面的測試。
4.1 使用傳統方法編寫一個方法,將id>10,name為zhang, age>20, gender為女,參加過ACM比賽的學生篩選出來,放入新的集合。在main中調用,然后輸出結果。


結果:

4.2 使用java8中的stream(), filter(), collect()編寫功能同4.1的函數,並測試。

public static List<Student> Search(List<Student> list){
		List<Student> t= list.stream().filter(e->e.getId()>10l&&e.getName().equals("zhang")
				&&e.getGender().equals(Gender.女)&&e.isJoinsACM()&&e.getAge()>20).collect(Collectors.toList());
		return t;
	}

功能一樣,結果一樣。

4.3 構建測試集合的時候,除了正常的Student對象,再往集合中添加一些null,然后重新改寫4.2,使其不出現異常。

public static List<Student> Search(List<Student> list){
		List<Student> t= list.stream().filter(e->e != null&&e.getId()>10l&&e.getName().equals("zhang")
				&&e.getGender().equals(Gender.女)&&e.isJoinsACM()&&e.getAge()>20).collect(Collectors.toList());
		return t;
	}

5.泛型類:GeneralStack(題目5-5)
5.1 截圖你的提交結果(出現學號)

5.2 GeneralStack接口的代碼

interface  GeneralStack <T>{
	T push( T item);            
	T pop();               
	T peek();               
	public boolean empty();
	public int size();

}

5.3 結合本題,說明泛型有什么好處

從本題來看,我們知道以前所定義IntegerStack接口,只能用於存放Integer類型的數據。然而對於棧來說,不管內部存放的是什么類型的數據,基本操作與元素的具體類型無關。這時就需要用到泛型,這樣就不用因為類的不同而去重復寫一樣的代碼。

6.泛型方法
基礎參考文件GenericMain,在此文件上進行修改。
6.1 編寫方法max,該方法可以返回List中所有元素的最大值。List中的元素必須實現Comparable接口。編寫的max方法需使得String max = max(strList)可以運行成功,其中strList為List<String>類型。也能使得Integer maxInt = max(intList);運行成功,其中intList為List<Integer>類型。

public static <T extends Comparable> T Max(List<T> list){
        T max= list.get(0);
        for(T e:list){
        	if(e.compareTo(max)>0)
        		max=e;
        }
        return max;
    }

3. 使用碼雲管理Java代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM