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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376704.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376704.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376704.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

對于enable_shared_from_this、shared_from_this使用筆記

文章為轉載匯總 參考&#xff1a;C11標準庫的一個工具類enable_shared_from_this的作用及原理分析 從這篇文章中可以知道&#xff1a; 當一個類public繼承enable_shared_from_this時&#xff0c;會獲得一個共有方法shared_from_this&#xff1a; class T : public enable_shar…

db file sequential read 事件的優化(一)

db file sequential read 事件的優化&#xff08;一&#xff09; db file sequential read等待事件有3個參數&#xff1a;file&#xff03;&#xff0c;first block&#xff03;&#xff0c;和block數量。在10g中&#xff0c;這等待事件受到用戶I/O等待級別的影響。當處理db fi…

2014-08-26 遇到的小問題

不能快速的保存bug的確是很不爽的事情 &#xff0c;不僅客戶著急 領導也著急 &#xff0c;自己也著急。。。。。。。。哈哈 原來好好的一個平臺 簡單的一個插入記錄 突然就報錯了 錯誤為 could not insert #9521 原來項目是用 NHibernate 做的插入 拋出的異常 真是無法定位到底…

Fatal error: Please read “Security“ section of the manual to find out how to run mysqld as root

.通過在命令后面加上–userroot 進行強制使用root賬號啟動。 cd /etc/init.d mysqld --userroot 參考&#xff1a; https://blog.csdn.net/huo_wa/article/details/117550307?spm1001.2101.3001.6650.2&utm_mediumdistribute.pc_relevant.none-task-blog-2%7Edefault%7ECT…

取消cp別名'cp -i'(unalias)

在rhel4updae8系統是使用cp命令覆蓋文件總是提示要輸入yes或no&#xff0c;即使加上-f參數也無法強行覆蓋。[2] 方法一&#xff1a; 輸入alias命令&#xff0c;看到系統內部使用的是cp的別名。 #alias alias cpcp -i 輸入unalias cp命令&#xff0c;解除別名。 unalias cp #&…

增加swap分區

在確定了服務器所需要使用的內存后&#xff0c;需要增加1024M的swap空間 具體操作&#xff1a; 1、dd if/dev/zero of/tmp/swap bs1M count1024M #創建1024M的文件塊 2、mkswap /tmp/swap #創建swap文件 3、swapon /tmp/swap #激活swap文件 4、swapon -s #查看swap 5、修改…

python 實現 topk算法

這里的版本是針對的一個class的某一個成員變量進行的&#xff1a; 關于如何定義對象的比較方法&#xff0c;請參考往期文章&#xff1a;python定義對象的比較方法 class province_room_quality_data:def __init__(self, room, quality):self.room roomself.quality qualityd…

2013年,未知的旅程

去年就開始策劃要辭職到外面去闖一闖&#xff0c;今年年初在上一家公司毅然辭職了&#xff0c;理由是自己需要出去鍛煉鍛煉。 帶著一個月的工資&#xff0c;和一點積蓄&#xff0c;還有一個女朋友來到了這個我以前都沒有來過的城市-深圳。 聽好多人都說過深圳是個好地方&#x…

SQL基礎

經過這幾天我才發現原來我最弱的是數據庫&#xff0c;好了現在就來補補吧 SQL(Struct Query Language) 結構化查詢語言&#xff0c;屬于第四代語言&#xff08;接近于自然語言&#xff09;符合主謂賓定狀補 DDL (Data Define L)數據定義語言,創建一個數據庫&#xff0c;創建一個…

主成分分析資料

推薦一份介紹主成分分析的資料&#xff1a;http://www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf&#xff0c;寫的很好&#xff01; 在R語言中使用PCA&#xff1a;http://www.cnblogs.com/bigshuai/archive/2012/06/18/2553808.html 轉載于:https://…

關于mysql的binlog寫滿磁盤而導致mysql無法連接的問題。

問題描述與追蹤 首先是mysql連不上&#xff1a; [rootVM-90-225-centos ~]# mysql -uroot -p Enter password: ERROR 2002 (HY000): Cant connect to local MySQL server through socket /var/lib/mysql/mysql.sock (111)然后查看mysql的狀態&#xff1a; [rootVM-90-225-c…

.NET系統架構改造的經驗和教訓

轉自&#xff1a; http://robbinfan.com/blog/43/rid-off-dotnet-experience 在互聯網行業&#xff0c;基于Unix/Linux的網站系統架構毫無疑問是當今主流的架構解決方案&#xff0c;這不僅僅是因為Linux本身足夠的開放性&#xff0c;更因為圍繞傳統Unix/Linux社區有大量的成熟開…

yum error :No module named yum

Yum&#xff08;全稱為 Yellow dog Updater, Modified&#xff09;是一個在Fedora和RedHat以及SUSE、CentOS中的Shell前端軟件包管理器。基於RPM包管理&#xff0c;能夠從指定的服務器自動下載RPM包并且安裝&#xff0c;可以自動處理依賴性關系&#xff0c;并且一次安裝所有依賴…

403報錯解決方案

403報錯解決方案 服務器使用yum install httpd時出現403報錯&#xff0c;參考了幾個文章&#xff1a; 首先根據這篇文章&#xff1a; devcloud上yum install 安裝軟件報錯403 打開 /root/.bashrc 文件&#xff0c;然后在最末尾可以看到 no_proxy&#xff1a; export no_pr…

控制器中獲取Field值

在ASP.NET MVC程序中&#xff0c;我們需要POST Data到制器中&#xff0c;是有很多方法。但是我們想在控制器中&#xff0c;獲取Feild值呢&#xff1f;怎樣獲取&#xff1f;你可以留意到有一個類FormCollection。它能幫助到我們解決這個問題。 舉個簡單的例子。在ASP.NET MVC應用…

new(std::nothrow)

new(std::nothrow) 顧名思義&#xff0c;即不拋出異常&#xff0c;當new一個對象失敗時&#xff0c;默認設置該對象為NULL&#xff0c;這樣可以方便的通過if(p NULL) 來判斷new操作是否成功 普通的new操作&#xff0c;如果分配內存失敗則會拋出異常&#xff0c;雖然后面一般也…

如何刪除cygwin

網上大多數方法在不具備用戶權限獲取的情況下都不能工作。 國外有人通過很簡單的命令行就實現了&#xff1a; Here’s how to remove Cygwin once and for all. You will need Cygwin cmd.exe 2 minutesRun cmd, navigate to C:\ (or other disk, if you have installed it in …

私有RTP協議和標準流媒體協議

先介紹下RTP協議&#xff1a; 實時傳輸協議RTP&#xff08;Real-time Transport Protocol&#xff09;是一個網絡傳輸協議 &#xff0c;該協議詳細說明了在互聯網上傳遞音頻和視頻的標準數據包格式 。 RTP標準定義了兩個子協議 &#xff0c;RTP和RTCP 數據傳輸協議RTP&#xff…