201521123072《java程序設計》第八週總結

1. 本週學習總結

1.1 以你喜歡的方式(思惟導圖或其餘)概括總結集合與泛型相關內容。
java

2. 書面做業

List中指定元素的刪除(題目4-1)

1.1 實驗總結
在刪除List中的元素中要考慮元素刪除後的位置(代碼以下:)編程

for (int i = 0; i < list.size(); i++) {
            if(list.get(i).equals(str)){
                list.remove(i);
                i--;
            }

統計文字中的單詞數量並按出現次數排序(題目5-3)

2.1 僞代碼(簡單寫出大致步驟)數組

while(has next input){   
            count=get 鍵值;
            if(input is"!!!!!")){
                break;
            }
            else 
              if(count==null){
                存入map映射中
              }
              else{
                 鍵值+1;
              }              
        }

2.2 實驗總結
這裏用到了匿名內部類來進行排序:
函數

倒排索引(題目5-4)

3.1 截圖你的提交結果(出現學號)
學習

3.2 僞代碼(簡單寫出大致步驟)
創建索引階段:測試

while(has next input){
            lineNumber++;行數+1      
        if(input is"!!!!!")){
                break;
            }
            else{  
               value=鍵值數組
                 if(value==null){  
                                      add 當前行數 in valuetemp
                          put in map
                 }
                    if(value contains 當前行數){
                        跳過餘下操做
                    }     
                      else{
add 當前行數 in value 
put in map
                      }                      
            }         
        }

查找階段網站

while(has next input){
           separate the key words;
             find the all key words 交集
     while(has next words)
          value[i].retainAll((value[i-1]));
         }
      output

3.3 實驗總結
1,讀一整句中的單詞用:code

Scanner sc=new Scanner(str);
                while(sc.hasNext()){
                      temp=sc.next(); 
}

2,o1.retainAll(o2);返回o1,o2對象中重合的部分並返回至o1對象

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中調用,而後輸出結果。
該方法爲:

放入的測試數據:

運行結果:
blog

4.2 使用java8中的stream(), filter(), collect()編寫功能同4.1的函數,並測試。
參考網站(http://ifeve.com/stream/

static List Selecte(List list){ 
        List<Student> newlist=new ArrayList();
        newlist=(ArrayList<Student>) list.parallelStream()
                .filter(student -> (((Student) student).getId() > 10L && ((Student) student).getName().equals("zhang")
                        && ((Student) student).getAge() > 20 && 
                        ((Student) student).getGender().equals(Gender.女)
                        && ((Student) student).isJoinsACM()))
                .collect(Collectors.toList());
        return newlist;
    }

運行結果:

4.3 構建測試集合的時候,除了正常的Student對象,再往集合中添加一些null,而後從新改寫4.2,使其不出現異常。
在數組中加入了null

只是在原先判斷基礎上,加入了student!=null

運行結果同上

泛型類:GeneralStack(題目5-5)

5.1 截圖你的提交結果(出現學號)

5.2 GeneralStack接口的代碼

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

用泛型編寫的代碼能夠被不一樣類型的對象所重用
題中的Integer,Double,Car均可以調用GeneralStack的實現類ArrayListGeneralStack
泛型容許指定集合中元素的類型
如:

這樣就能夠無需使用有風險的強制類型轉換,錯誤在編譯階段就能發現

泛型方法

基礎參考文件GenericMain,在此文件上進行修改。
6.1 編寫方法max,該方法能夠返回List中全部元素的最大值。List中的元素必須實現Comparable接口。編寫的max方法需使得String max = max(strList)能夠運行成功,其中strList爲List 類型。也能使得Integer maxInt = max(intList);運行成功,其中intList爲List 類型。
代碼以下:

運行結果:

6.2 選作:現有User類,其子類爲StuUser,且均實現了Comparable接口。編寫方法max1,基本功能同6.1,並使得max1(stuList);能夠運行成功,其中stuList爲List 類型。

運行結果:

6.3 選作:編寫int myCompare(T o1, T o2, Comparator c)方法,該方法能夠比較User對象及其子對象,傳入的比較器c既能夠是Comparator ,也能夠是Comparator 。注意:該方法聲明未寫全,請自行補全。

3. 碼雲上代碼提交記錄及PTA實驗總結

題目集:jmu-Java-05-集合

3.1. 碼雲代碼提交記錄

3.2. PTA實驗

函數(4-1),編程(5-3,5-4,5-5) 實驗總結已經在做業中體現,不用寫。

相關文章
相關標籤/搜索