日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA_Collection容器

發布時間:2023/12/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA_Collection容器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  因為項目的需要,今天抽時間把JAVA中的容器復習了一下,為了以后的不時之需,現在把它記下來。

  容器有其名,知其意,用來盛放數據的集合,JAVA中為我們提供了三種容器類:set、list、map,三種容器之間既有聯系又有區別,首先它們均繼承了Collection容器,區別在于:set容器存儲數據類似于集合,里面的數據之間沒有順序,不能重復;list容器中的數據有序,并且數據可以重復;最后map容器是一種通過鍵值對進行的存儲,所以map容器要求鍵值不能重復。

  通過這個圖相信大家一定能夠對JAVA容器有一個很好地認識。

接下來讓我們一起看幾個例子:

第一個: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());}} }

輸出結果:(HashSet存儲里面的數據是無序的)

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());}} }

輸出結果:

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());}} }

輸出結果:

public class object_interator {public static void main(String [] args){Collection c = new ArrayList();//特別注意,add添加的均要為Object對象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()獲得的是一個Object對象,一定要轉化為指定的對象,然后進行操作System.out.println(stu);//默認調用其toString()方法 }} }//定義的一個student對象 class student{public String name;public String sex;//無參構造方法public student(){}//有參構造方法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;} }

下面簡單介紹一下SDK1.5提出的增強for循環:

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("傳統的輸出:"+arr[i]);}System.out.println("");for(int i : arr){System.out.println("增強的for循環輸出:"+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);//默認調用其toString()方法 }} }

對于List容器JAVA給出了一種處理內部數據的方法:Collections,下面簡單給大家分享一下我的理解:

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);//隨機排列 System.out.println(li);Collections.sort(li);//排序 System.out.println(li);int n = Collections.binarySearch(li, "a5");//基于二分法的查找System.out.println("a5的位置:"+n);} }

輸出結果:

到這里我想大家估計已經對容器有了一定的了解,如果你有更好的認識還望大家賜教。

轉載于:https://www.cnblogs.com/AndroidJotting/p/3935959.html

總結

以上是生活随笔為你收集整理的JAVA_Collection容器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。