java集合練習題

來源:互聯網
上載者:User

習題1、

每個學生都有對應的歸屬地,學生Student,地址String類型

學生屬性:姓名和年齡

註:姓名和年齡相同的是為同一個學生,保證學生的唯一性

import java.util.*;class MapDemo {public static void main(String[] args) {HashMap<Student,String> hm=new HashMap<Student,String>();//建立HashMap容器來儲存資訊hm.put(new Student("zjd",12),"山東");//添加資訊hm.put(new Student("zxm",12),"山東");hm.put(new Student("zjn",12),"山東");hm.put(new Student("dxm",12),"山東");hm.put(new Student("wyj",12),"山東");hm.put(new Student("zxz",12),"山東");hm.put(new Student("zjj",12),"山東");hm.put(new Student("zjd",12),"山東");hm.put(new Student("zjn",12),"山東");hm.put(new Student("dxm",12),"山東");hm.put(new Student("wyj",12),"山東");Set<Map.Entry<Student,String>> s=hm.entrySet();//使用entrySet來實現HashMap轉化為Set集合Iterator<Map.Entry<Student,String>> it=s.iterator();while(it.hasNext()){Map.Entry<Student,String> me=it.next();Student stu=me.getKey();String address=me.getValue();sop(stu.getName()+":"+stu.getAge()+":"+address+"entrySet");      }        Set<Student> s1=hm.keySet();//使用keySet來實現HashMap轉化為Set集合Iterator<Student> it1=s1.iterator();while(it1.hasNext()){Student stu1=it1.next();sop(stu1.getName()+":"+stu1.getAge()+":"+hm.get(stu1)+"keySet");}}public static void sop(Object obj)     {System.out.println(obj);}}/*建立學生類Student姓名:name年齡:age*/class Student{private String name;private int age;Student(String name,int age){//建立建構函式實現初始化this.name=name;this.age=age;}public void setStudent(String name,int age){//建立設定學生資訊方法this.name=name;this.age=age;}public String getName(){//擷取學生姓名return this.name;}public int getAge(){//擷取學生年齡return this.age;}public int hashCode(){//覆蓋hashCode讓其按照Student中的資訊進行判斷return this.name.hashCode()+this.age*37;}public boolean equals(Object obj){//覆蓋equals來判斷姓名和年齡是否都相同if(!(obj instanceof Student))//判斷傳入的是否是Student類型return false;Student s=(Student)obj;return this.name.equals(s.name)&&this.age==s.age;}}

練習2、

擷取一個字串的字母出現的次數:

import java.util.*;class TestDemo1 {public static void main(String[] args) {String str="sdfgesdgsdxjahskjsaxkskfdsfcbxbbfhj";char[] ch=str.toCharArray();//將字串轉化為字元數組TreeMap<Character,Integer> m=new TreeMap<Character,Integer>();//這地方選用TreeMap,因為他可以自動排序for(char c:ch){Integer i=m.get(c);//查看是否存在if(i==null){m.put(c,1);}else{m.put(c,i+1);}}System.out.println(m);//直接輸出}}

集合擴充知識:

Collections類這個類提供了多種操作集合的方法例如

Collections.max()//返回Collection集合中的最大值

Collections.sort()//對List集合進行排序

Collections.binarySearch()//返回List集合相對的值

Collections.fill()//將List集合中的所有元素替換成指定元素

Collections.shuffle()//將List集合中資料重新隨機排序

import java.util.*;class CollectionsTest {public static void main(String[] args) {ArrayList<String> al=new ArrayList<String>();//建立一個ArrayList集合al.add("asdasd");al.add("sdxa");al.add("zjdassa");al.add("asdasd");al.add("wdad");al.add("rdfgx");for(Iterator it=al.iterator();it.hasNext();){//遍曆結果System.out.println(it.next());}System.out.println("排序後:");Collections.sort(al);for(Iterator it=al.iterator();it.hasNext();){System.out.println(it.next());}sop("輸出最大值:");sop(Collections.max(al));sop("打亂順序");Collections.shuffle(al);for(Iterator it=al.iterator();it.hasNext();){System.out.println(it.next());}}public static void sop(Object obj){System.out.println(obj);}}

增強for迴圈也叫進階for迴圈

格式:for(數群組類型變數 變數名:被遍曆的集合(Collection)或數組)

JDK1.5新特性

1、可變參數:使用時一定要注意可變參數一定要放在參數列表的最後面

2、靜態匯入:當類重名是,需要指定具體的包名,當方法重名時,需要指定具備附屬的對象或者類

格式:import static  包類.*;

靜態匯入只能匯入類而不能匯入方法;




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.