HashSet

數據的插入,刪除,顯示。Set是無序的。

 

  1. package com.test2;  
  2.  
  3. import java.util.HashSet;  
  4. import java.util.Iterator;  
  5. import java.util.Scanner;  
  6.  
  7. public class Demo8 {  
  8.  
  9.     /**  
  10.      * @param args  
  11.      */ 
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub  
  14.  
  15.         System.out.println("請輸入學生姓名:");  
  16.         Scanner sc = new Scanner(System.in);  
  17.         HashSet<String> stu = new HashSet<String>();  
  18.  
  19.         boolean b = true;  
  20.         while (b) {  
  21.             String s = sc.nextLine();  
  22.             if (s.length() > 0) {  
  23.                 stu.add(s);  
  24.             } else {  
  25.                 b = false;  
  26.             }  
  27.  
  28.         }  
  29.         System.out.println("請輸入要刪除學生的姓名 ");  
  30.         b = true;  
  31.         while (b) {  
  32.             String s = sc.nextLine();  
  33.  
  34.             if (s.length() > 0) {  
  35.                 if (stu.remove(s)) {  
  36.                     System.out.println("刪除成功");  
  37.                 } else {  
  38.  
  39.                     System.out.println("沒有找到此人");  
  40.                 }  
  41.             } else {  
  42.                 b = false;  
  43.             }  
  44.         }  
  45.         System.out.println("剩下的學生:");  
  46.         Iterator iterator = stu.iterator();  
  47.         while (true) {  
  48.             if (iterator.hasNext()) {  
  49.                 System.out.println(iterator.next().toString());  
  50.             } else {  
  51.                 break;  
  52.             }  
  53.         }  
  54.         sc.close();  
  55.     }  
  56. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章