JAVA_Collection容器
因?yàn)轫?xiàng)目的需要,今天抽時(shí)間把JAVA中的容器復(fù)習(xí)了一下,為了以后的不時(shí)之需,現(xiàn)在把它記下來(lái)。
容器有其名,知其意,用來(lái)盛放數(shù)據(jù)的集合,JAVA中為我們提供了三種容器類:set、list、map,三種容器之間既有聯(lián)系又有區(qū)別,首先它們均繼承了Collection容器,區(qū)別在于:set容器存儲(chǔ)數(shù)據(jù)類似于集合,里面的數(shù)據(jù)之間沒(méi)有順序,不能重復(fù);list容器中的數(shù)據(jù)有序,并且數(shù)據(jù)可以重復(fù);最后map容器是一種通過(guò)鍵值對(duì)進(jìn)行的存儲(chǔ),所以map容器要求鍵值不能重復(fù)。
通過(guò)這個(gè)圖相信大家一定能夠?qū)AVA容器有一個(gè)很好地認(rèn)識(shí)。
接下來(lái)讓我們一起看幾個(gè)例子:
第一個(gè):HashSet、LinkedList、ArrayList、Interator的介紹
public class hashset {public static void main(String[] args) {Collection c = new HashSet();c.add("one");c.add("two");c.add("three");c.add("four");c.add("five");Iterator it = c.iterator();while(it.hasNext()){System.out.println(it.next());}} }輸出結(jié)果:(HashSet存儲(chǔ)里面的數(shù)據(jù)是無(wú)序的)
public class linkedlist {public static void main(String[] args) {Collection c = new LinkedList();c.add("one");c.add("two");c.add("three");c.add("four");c.add("five");Iterator it = c.iterator();while(it.hasNext()){System.out.println(it.next());}} }輸出結(jié)果:
public class hashset {public static void main(String[] args) {Collection c = new HashSet();c.add("one");c.add("two");c.add("three");c.add("four");c.add("five");Iterator it = c.iterator();while(it.hasNext()){System.out.println(it.next());}} }輸出結(jié)果:
public class object_interator {public static void main(String [] args){Collection c = new ArrayList();//特別注意,add添加的均要為Object對(duì)象c.add(new student("張生", "男"));c.add(new student("王二", "男"));c.add(new student("莉莉", "女"));c.add(new student("小明", "男"));Iterator it = c.iterator();while(it.hasNext()){student stu = (student)it.next();//特別注意it.next()獲得的是一個(gè)Object對(duì)象,一定要轉(zhuǎn)化為指定的對(duì)象,然后進(jìn)行操作System.out.println(stu);//默認(rèn)調(diào)用其toString()方法 }} }//定義的一個(gè)student對(duì)象 class student{public String name;public String sex;//無(wú)參構(gòu)造方法public student(){}//有參構(gòu)造方法public student(String name, String sex){this.name = name;this.sex = sex;}public String getname(){return name;}public String getsex(){return sex;}//從寫其toString()方法public String toString(){return "姓名:"+name+" 性別:"+sex;} }下面簡(jiǎn)單介紹一下SDK1.5提出的增強(qiáng)for循環(huán):
public class addFor {public static void main(String[] args) {int arr [] = {1,2,3,4,5};for(int i=0; i<arr.length;i++){System.out.println("傳統(tǒng)的輸出:"+arr[i]);}System.out.println("");for(int i : arr){System.out.println("增強(qiáng)的for循環(huán)輸出:"+i);}System.out.println("");Collection c = new ArrayList();c.add(new String("aaa"));c.add(new String("bbb"));c.add(new String("ccc"));c.add(new String("ddd"));for(Object o : c){System.out.println(o);//默認(rèn)調(diào)用其toString()方法 }} }對(duì)于List容器JAVA給出了一種處理內(nèi)部數(shù)據(jù)的方法:Collections,下面簡(jiǎn)單給大家分享一下我的理解:
public class list_fix {public static void main(String [] args){List li = new ArrayList();for(int i = 0; i<=5; i++){li.add("a"+i);}System.out.println("處理前:"+li);Collections.reverse(li);//逆序排列 System.out.println(li);Collections.shuffle(li);//隨機(jī)排列 System.out.println(li);Collections.sort(li);//排序 System.out.println(li);int n = Collections.binarySearch(li, "a5");//基于二分法的查找System.out.println("a5的位置:"+n);} }輸出結(jié)果:
到這里我想大家估計(jì)已經(jīng)對(duì)容器有了一定的了解,如果你有更好的認(rèn)識(shí)還望大家賜教。
轉(zhuǎn)載于:https://www.cnblogs.com/AndroidJotting/p/3935959.html
總結(jié)
以上是生活随笔為你收集整理的JAVA_Collection容器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 蔡幸娟为何在大陆不火?
- 下一篇: 2014-08-26 遇到的小问题