集合簡概
集合框架學習理由:
Java是面向物件的語言,Java的大部分應用都涉及到對儲存物件的增,刪,查,改操作。
陣列可以用來儲存多個物件,由於陣列的長度固定,對於物件的增刪改操作非常麻煩,而集合類則能夠滿足我們的需求。
陣列和集合的區別:
長度
陣列的長度固定不變,不能做更改。
集合長度是可以根據實際需要做更改。
內容
陣列儲存的必須是同一種類型的元素。
集合可以儲存不同型別的元素,沒有限定。
資料型別
陣列可以儲存基本資料型別,也可以儲存引用資料型別。
集合只能儲存引用型別,雖然不能儲存基本資料型別,但是可以儲存基本資料型別的包裝類型別。
集合是一套框架,必須具備以下方法,且頂層父類應該設計成介面,用來完成資料的增,刪,查,改操作。
特點:
1.集合是有序的(儲存有序)
2.有索引,方便查詢和修改
3.List集合可重複
4.允許儲存null值
所有方法:
新增
boolean add(Object obj) 往集合裡增加某個元素;
boolean addAll(Collection c) 將另一個集合增加到某一個集合裡;
void add(int index, E element) 從某節點插入某一個元素;
boolean addAll(int index, Collection<? extends E> c)
刪除
void clear() 清除集合內所有元素;
boolean remove(Object o) 移除某一個元素;
boolean removeAll(Collection<?> c) 移除集合內所有元素;
Object remove(int index) 從某節點移除某一個元素;
修改
Object set(int index, E element)
遍歷
Object[] toArray()
Iterator<E> iterator()
<T> T[] toArray(T[] a)
ListIterator<E> listIterator()
ListIterator<E> listIterator(int index)
判斷
boolean contains(Object o) 是否含有某一個元素
boolean containsAll(Collection<?> c) 是否含有某個集合
boolean isEmpty() 集合是否為空
其他
boolean retainAll(Collection<?> c)
獲取
int size() 獲取集合長度;
int indexOf(Object o)
Object get(int index)
int lastIndexOf(Object o)
List subList(int fromIndex, int toIndex)
應用案例:
import java.awt.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class kookv0427 {
private static Object Idol;
public static void main(String[] args) {
// 建立容器
Collection c1 = new ArrayList();
// 建立愛豆元素物件:姓名,年齡,定位
Idol i1 = new Idol("金南俊",26,"rap");
Idol i2 = new Idol("金碩珍",28,"vocal");
Idol i3 = new Idol("閔玧其",27,"rap");
Idol i4 = new Idol("鄭號錫",26,"rap");
Idol i5 = new Idol("樸智旻",25,"vocal");
Idol i6 = new Idol("金泰亨",25,"vocal");
Idol i7 = new Idol("田柾國",23,"vocal");
Idol i8 = new Idol("方時赫",48,"PD");
Idol i9 = new Idol("方時赫",48,"PD");
// 將元素物件儲存到集合中
c1.add(i1);
c1.add(i2);
c1.add(i3);
c1.add(i4);
c1.add(i5);
c1.add(i6);
c1.add(i7);
c1.add(i8);
c1.add(i9);
// 遍歷集合
Iterator it = c1.iterator();
while (it.hasNext()) {
Object ob = it.next();
Idol i = (Idol) ob;
System.out.println(i);
}
// 去除重複元素
// 1、建立新容器
Collection c2 = new ArrayList();
// 2、遍歷舊集合
for (Object ob : c1) {
// 3、判斷新集合是否存在這個元素
if (!c2.contains(ob)) {
// 4、如果新集合不存在這個元素就儲存到新集合中
c2.add(ob);
}
}
System.out.println("--------------------------------");
// 5、遍歷新集合檢視是否去除重複元素
for (Object object : c2) {
System.out.println(object);
}
System.out.println("--------------------------------");
// 6、移除方時赫!
System.out.println(c2);
System.out.println("remove:" + c2.remove(i8));
for (Object object : c2) {
System.out.println(object);
}
}
}
class Idol{
private String name;
private Integer age;
private String work;
public String toString() {
return "Idol [name=" + name + ", age=" + age + ", work=" + work + "]";
}
public Idol(String name, Integer age, String work) {
super();
this.name = name;
this.age = age;
this.work = work;
}
public Idol() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Idol other = (Idol) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true