JAVA复习5(集合——ArrayList)
- 集合
?
所謂集合指的就是一套動態對象數組,在實際開發中數組的概念的一定會使用的,但是數組的問題是一旦開辟空間則長度不可改變
?
?
其實就是對數據結構的一種封裝,用戶不用去編寫,直接使用。 由于數據結構開發起來比較困難,還必須考慮性能問題
?
?
?
3.1 集合中需要掌握的核心接口
?
Collection? List? Set? Map? Iterator?? (了解) Enumeration? Queue
?
?
3.2 List接口
?
List接口的定義:
| public interface List<E> extends Collection<E> |
?
通過觀察List接口的定義其繼承的是Collection 接口
通過觀察接口之間的關系,可以得出上圖
?
觀察Collection常用方法
?
1 public? boolean?add(E?e);??? 增加元素到集合
?
2 public boolean?addAll(Collection<? extends E>?c); 存放一個集合
?
3 public boolean?contains(Object?o); 查找集合中的元素
?
4 public boolean?isEmpty() ; 判斷一個集合是否為空
?
5 public boolean?remove(Object?o) 刪除一個集合中的元素
?
6 public int?size(); 返回集合中的長度
?
觀察List接口中的方法
?
List擴展Collection中的方法
1public E?get(int?index); 根據指定索引取得元素
?
2 public E?set(int?index, E?element) ;? 替換元素, index要替換元素下標? element要替換的元素
?
3 public ListIterator<E>?listIterator() List自己的迭代器
?
List接口的特點: 可重復的, 有序的
?
使用List list本身是一個接口,如果想要使用一個接口則可以使用該接口的實現類完成
List下面的實現類:
?
需要掌握的實現類 : ArrayList?? LindkedList? Vector
?
范例: 使用List 接口
| ???? public static void main(String[] args) { ????????? // 泛型的使用中 只能使用類 基本數據類型不行 ????????? ????????? List<Integer> array=new ArrayList<>(); ????????? ????????? array.add(4);? //賦值 ????????? ????????? array.add(3); ????????? ????????? array.add(3); ????????? ????????? array.add(1); ????????? ????????? ????????? for(int i=0;i<array.size();i++) { ?????????????? ?????????????? System.out.println(array.get(i));? //通過get取值 ????????? } ???? } |
通過使用ArrayList發現 其特點是 可重復的,并且有序的,順序就存儲時候的順序
通過一個 add 增加元素到集合?? 通過get(index) 取出集合中的元素? 下標的位置從0開始
?
觀察其中的一些其他的操作方法:
?
1 判斷集合是否為空? pulic boolean isEmpty();
?
2 取得集合中的長度 pulic int size();
?
3 刪除集合中的元素 public boolean remove(Object obj);
?
| public static void main(String[] args) { ????????? // 泛型的使用中 只能使用類 基本數據類型不行 ????????? ????????? List<Integer> array=new ArrayList<>(); ????????? ????????? ????????? System.out.println("&&&&&"+array.isEmpty()); ????????? ????????? array.add(4);? //賦值 ????????? ????????? array.add(3); ????????? ????????? array.add(3); ????????? ????????? System.out.println(array.remove(1)); ????????? array.add(1); ????????? System.out.println(array.isEmpty()); ????????? ????????? System.out.println(array); ???? } |
?
通過觀察源碼發現ArrayList 是一個對象數組, 每次增加的時候 會為數組擴容,數組長度是不能改變的,每次擴容數組內容拷貝的工作 ,ArrayList如果頻繁增加內容,效率不高, 但是查詢的時候由于底層使用的是數組,所以查詢效率會高
?
面試題:
?
?
ArrayList保存自定義類:
?
首先觀察使用系統自定義的類完成ArrayList類的添加
| public static void main(String[] args) { ????????? // 泛型的使用中 只能使用類 基本數據類型不行 ????????? ????????? List<String> array=new ArrayList<>(); ????????? ????????? array.add("A"); ????????? ????????? array.add("B"); ????????? ????????? array.add("C"); ????????? ????????? System.out.println(array.contains("C")); ????????? for(String s:array) { ?????????????? ?????????????? System.out.println(s); ????????? } ????????? ???? } |
?
以上使用的類 為系統自定義的String類 其類功能已經非常完善了,現在使用用戶自定義的類完成ArrayList的添加
范例:實現自定義類
| package org.list; ? public class Person { ? ???? private String name; ???? ???? private int age; ???? ???? public Person(String name,int age) { ????????? ????????? this.name=name; ????????? ????????? this.age=age; ???? } ? ???? public String getName() { ????????? return name; ???? } ? ???? public void setName(String name) { ????????? this.name = name; ???? } ? ???? public int getAge() { ????????? return age; ???? } ? ???? public void setAge(int age) { ????????? this.age = age; ???? } ? ???? @Override??????????? //new Person("張三",20) ???? public boolean equals(Object obj) {??? // 傳進來要比較的內容 ????????? // TODO Auto-generated method stub ????????? ????????? Person per=null;?? ????????? if(obj instanceof Person) { ?????????????? ?????????????? per=(Person)obj;? // Object 向下轉型 為 Person類型 ????????? } ????????? // this 當前的對象 Person? 和 傳進來的Person比較 ????????? if(this==per) {? //內存地址 一樣肯定是同樣對象 ?????????????? ?????????????? return true; ????????? } ????????? ????????? if(this.age==per.age&&this.name.equals(per.name)) { ?????????????? ?????????????? return true; ????????? } ????????? ????????? return false; ???? } ???? ???? ???? } ? |
?
通過代碼,發現自定義類的時候,必須覆寫equals方法才能完成集合中 對象查找和刪除,主要原因是在于進行對象刪除或者查找的時候 集合中會判斷傳入的元素和集合本身的元素是否是內容相同的元素 只有相同才會刪除或者查找
總結
以上是生活随笔為你收集整理的JAVA复习5(集合——ArrayList)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在Win10中检查磁盘驱动器错误
- 下一篇: 利用java实现一个简单的远程监控程序