Java---25---集合架構共性方法

來源:互聯網
上載者:User

標籤:arraylist   儲存   資料結構   架構   

集合類

為什麼會出現集合類

 

物件導向語言對事物的體現都是以對象的形式,所以為了方便對較多個對象的操作,就對對象進行儲存,集合就是儲存物件最常用的一種方式

 

數組和集合類同時容器,有何不同?

數組雖然也可以儲存物件,但是長度不可變;集合的長度是可變的,數組中可以儲存基礎資料型別 (Elementary Data Type),集合只能儲存物件。

 

集合類的特點:

集合只用於儲存物件,集合長度是可變的,集合可以儲存不同類型的對象。

為什麼會出現這麼多的容器呢?

因為每一個容器對資料的儲存方式都有不同

這個儲存方式稱之為:資料結構

 

 

共性方法:(Collection 的方法)

增、刪、改、查。

方法摘要

 boolean

add(E e) 
          確保此 collection 包含指定的元素(可選操作)。

 boolean

addAll(Collection<? extends E> c) 
          將指定 collection 中的所有元素都添加到此 collection 中(可選操作)。

 void

clear() 
          移除此 collection 中的所有元素(可選操作)。

 boolean

contains(Object o) 
          如果此 collection 包含指定的元素,則返回 true。

 boolean

containsAll(Collection<?> c) 
          如果此 collection 包含指定 collection 中的所有元素,則返回 true。

 boolean

equals(Object o) 
          比較此 collection 與指定對象是否相等。

 int

hashCode() 
          返回此 collection 的雜湊碼值。

 boolean

isEmpty() 
          如果此 collection 不包含元素,則返回 true。

 Iterator<E>

iterator() 
          返回在此 collection 的元素上進行迭代的迭代器。

 boolean

remove(Object o) 
          從此 collection 中移除指定元素的單個執行個體,如果存在的話(可選操作)。

 boolean

removeAll(Collection<?> c) 
          移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。

 boolean

retainAll(Collection<?> c) 
          僅保留此 collection 中那些也包含在指定 collection 的元素(可選操作)。

 int

size() 
          返回此 collection 中的元素數。

 Object[]

toArray() 
          返回包含此 collection 中所有元素的數組。

<T> T[]

 

toArray(T[] a) 
          返回包含此 collection 中所有元素的數組;返回數組的運行時類型與指定數組的運行時類型相同。

 

 

 

add 方法的參數類型是Object 以便於接收任意類型的對象

集合中儲存的都是對象的引用(地址)



import java.util.ArrayList;public class Main {public static void main(String[] args) {//method_1();method_2();}public static void method_2() {ArrayList al1 = new ArrayList();ArrayList al2 = new ArrayList();al1.add("abc01");al1.add("abc02");al1.add("abc03");al1.add("abc04");al2.add("abc01");al2.add("abc02");al2.add("abc05");al2.add("abc06");al1.removeAll(al2);//去除al1與al2的交集,儲存在al1中sop(al1);sop(al2);/* * 列印結果: * [abc03, abc04] *  * [abc01, abc02, abc05, abc06] *  * */}public static void method_1() {// 建立一個集合容器,使用Collection介面的子類ArrayListArrayList a1 = new ArrayList();// 添加元素a1.add("abc01");a1.add("abc02");a1.add("abc03");a1.add("abc04");// 擷取個數 集合長度System.out.println(a1.size());// 列印集合System.out.println(a1);// 刪除元素a1.remove("abc03");System.out.println(a1);/* * 列印結果 4 [abc01, abc02, abc03, abc04] [abc01, abc02, abc04] */// a1.clear();//清空集合// 判斷某一元素是否在集合中System.out.println(a1.contains("abc01"));// 判斷集合是否為空白System.out.println(a1.isEmpty());}public static void sop(Object obj) {System.out.println(obj);}}


Java---25---集合架構共性方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.