集合的由来:
我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储。而要想存储多个对象,就不能是一个基本的变量,而应该是一个容器类型的变量,在我们目前所学过的知识里面,有数组和StringBuffer。但是StringBuffer的结果是一个字符串,不一定满足我们的要求,所以我们只能选择数组,这就是对象数组。而对象数组又不能适应变化的需求,因为数组的长度是固定的,这个时候,为了适应变化的需求,Java就提供了集合类供我们使用。
数组和集合的区别?
A:长度区别
数组的长度固定
集合长度可变
B:内容不同
数组存储的是同一种类型的元素
而集合可以存储不同类型的元素
C:元素的数据类型问题
数组可以存储基本数据类型,也可以存储引用数据类型
集合只能存储引用类型
集合的继承体系结构:
集合是存储多个元的,但是存储多个元素我们也是有不同需求的:比如说,我要这多个元素中不能有相同的元素,再比如说,我要这多个元素按照某种规则排序一下。针对不同的需求,Java就提供了不同的集合类,这样,Java就提供了很多个集合类。这些集合类的数据结构不同,结构不同不重要的,重要的是你要能够存储东西,并且还要能够使用这些东西,比如说判断,获取等。所以多个集合类是有共性的内容的,我们把这些集合类的共性内容不断的向上提取,最终就能形成集合的继承体系结构。
数据结构:
数据的存储方式。
Collection接口的功能概述:
1:添加功能
boolean add(Object obj):添加一个元素
boolean addAll(Collection c):把指定集合中的元素全部添加到原集合中
2:删除功能
void clear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):在原集合中找与指定集合对应的元素,有就删除,删除一个就返回true
3:判断功能
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):原集合中包含指定集合中多有的元素才返回true
boolean isEmpty():判断集合是否为空
4:获取功能
Iterator<E> iterator():获取迭代器方法
5:长度功能
int size():元素的个数
6:交集功能
boolean retainAll(Collection c):原集合与指定集合对比,删除不同的,留下相同的元素,原集合发生变化就返回true
7:把集合转换为数组
Object[] toArray() 把集合转成字符串
List集合的特有功能:
A:添加功能
void add(int index,Object element):在指定位置添加元素
B:获取功能
Object get(int index):获取指定位置的元素
C:列表迭代器
ListIterator listIterator():List集合特有的迭代器
D:删除功能
Object remove(int index):根据索引删除元素,返回被删除的元素
E:修改功能
Object set(int index,Object element):根据索引修改元素,返回被修饰的元素
列表迭代器:
ListIterator listIterator():List集合特有的迭代器
该迭代器继承了Iterator迭代器,所以,就可以直接使用hasNext()和next()方法。
特有功能:
Object previous():获取上一个元素
boolean hasPrevious():判断是否有元素
注意:ListIterator可以实现逆向遍历,但是必须先正向遍历,才能逆向遍历,所以一般无意义,不使用。
并发修改异常解决方式
A:迭代器迭代元素,迭代器修改元素
元素是跟在刚才迭代的元素后面的。
B:集合遍历元素,集合修改元素(普通for)
元素在最后添加的。
List的子类特点
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList:
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
List子类使用规则
要安全吗?
要:Vector(即使要安全,也不用这个了,后面有替代的)
不要:ArrayList或者LinkedList
查询多:ArrayList
增删多:LinkedList
ArrayList
A:没有特有功能
a:ArrayList存储字符串并遍历
b:ArrayList存储自定义对象并遍历
Vector
A:有特有功能
a:添加
public void addElement(E obj) -- add()
b:获取
public E elementAt(int index) -- get()
public Enumeration<E> elements() -- iterator()
B:案例
a:Vector存储字符串并遍历
b:Vector存储自定义对象并遍历
LinkedList
A:有特有功能
a:添加
addFirst()
addLast()
b:删除
removeFirst()
removeLast()
c:获取
getFirst()
getLast()
B:案例
a:LinkedList存储字符串并遍历
b:LinkedList存储自定义对象并遍历
数组有没有length()方法呢?
数组是length属性
字符串有没有length()方法呢?
字符串对象.length()
集合有没有length()方法呢?
集合对象.set()
JDK升级的原因:
A:安全
B:效率
C:简化书写
案例一:练习集合中不带All的方法
public class CollectionDemoOne {
public static void main(String[] args) {
// 创建集合对象
// Collection c = new Collection(); //错误,因为接口不能实例化
Collection c = new ArrayList();
c.add("hello"):添加一个元素
System.out.println("add:"+c.add("hello"));
c.clear():移除所有元素
System.ouot.println("元素全部删掉了"+c);
c.add("java");
remove("java"):移除一个元素
System.out.println("remove:"+remove("java"));
c.add("world");
c.contains("world"):判断集合中是否包含指定的元素
System.out.println("contains:"+c.contains("world"));
c.isEmpty():判断集合是否为空
System.out.println("isEmpty:"+c.isEmpty());
c.size():元素的个数
System.out.println("size:"+c.size());
}
}
案例二:练习集合中带All的方法
public class CollectionDemoTwo {
public static void main(String[] args) {
// 创建集合1
Collection c1 = new ArrayList();
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c1.add("abc4");
// 创建集合2
Collection c2 = new ArrayList();
c2.add("abc4");
c2.add("abc5");
c2.add("abc6");
c2.add("abc7");
System.out.println("addAll:" + c1.addAll(c2));//添加一个集合的元素
System.out.println("removeAll:"+c1.removeAll(c2));//只要有一个元素被移除了,就返回true。
System.out.println("containsAll:"+c1.containsAll(c2));//只有包含所有的元素,才叫包含
System.out.println("retainAll:"+c1.retainAll(c2));
//假设有两个集合A,B。
//A对B做交集,最终的结果保存在A中,B不变。
//返回值表示的是A是否发生过改变。
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
}
}
案例三:集合(存储字符串)遍历,两种方法
public class CollectionDemoThree {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 添加元素
c.add("hello");
c.add("world");
c.add("java");
// 遍历
Object[] objs = c.toArray();把集合转成数组,可以实现集合的遍历
for (int x = 0; x < objs.length; x++) {
String s = (String) objs[x];
System.out.println(s + "---" + s.length());
}
Iterator it = c.iterator();//迭代器,集合的专用遍历方式
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
案例四:遍历集合(存储字符串)遍历,两种方法
public class IteratorTestOne {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("赵云", 27);
Student s2 = new Student("张飞", 30);
Student s3 = new Student("关羽", 33);
// 把学生添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
// 遍历
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
Object[] objs = c.toArray();把集合转成数组,可以实现集合的遍历
for (int x = 0; x < objs.length; x++) {
String s = (String) objs[x];
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
public class Student{
...标准学生类
}
案例五:List集合存储字符串并遍历。
List集合的特点:有序(存储和取出的元素一致),可重复的
public class ListDemoOne {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 创建字符串并添加字符串
list.add("hello");
list.add("world");
list.add("java");
list.add("hello");
list.add("world");
list.add("java");
// 遍历集合
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
案例六:List集合存储自定义对象并遍历。
public class ListDemoTwo {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 创建学生对象
Student s1 = new Student("夏侯惇", 30);
Student s2 = new Student("夏侯渊", 40);
Student s3 = new Student("许褚", 22);
// 把学生对象添加到集合对象中
list.add(s1);
list.add(s2);
list.add(s3);
// 遍历
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
public class Student{
...标准学生类
}
案例七:练习List集合的特有功能以及列表迭代器
public class ListDemoThree {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
list.add(1, "android");//在指定位置添加元素
System.out.println("get:" + list.get(1));获取指定位置的元素
System.out.println("remove:" + list.remove(1));根据索引删除元素,返回被删除的元素
System.out.println("set:" + list.set(1, "javaee"));根据索引修改元素,返回被修饰的元素
// 遍历方式:size()和get()
for (int x = 0; x < list.size(); x++) {
String s = (String) list.get(x);
System.out.println(s);
}
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
ListIterator lit = list.listIterator();
while (lit.hasPrevious()) {
String s = (String) lit.previous();
System.out.println(s);
}
System.out.println("list:" + list);
}
}
案例八:并发修改异常
public class ListIteratorDemo {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// 方式1:迭代器迭代元素,迭代器修改元素
ListIterator lit = list.listIterator();
while (lit.hasNext()) {
String s = (String) lit.next();
if ("world".equals(s)) {
lit.add("javaee");
}
}
// 方式2:集合遍历元素,集合修改元素(普通for)
for (int x = 0; x < list.size(); x++) {
String s = (String) list.get(x);
if ("world".equals(s)) {
list.add("javaee");
}
}
System.out.println("list:" + list);
}
}
案例九:ArrayList的使用,存储字符串并遍历
public class ArrayListDemo {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
// 创建元素对象,并添加元素
array.add("hello");
array.add("world");
array.add("java");
// 遍历
Iterator it = array.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
for (int x = 0; x < array.size(); x++) {
String s = (String) array.get(x);
System.out.println(s);
}
}
}
案例十:Vector的使用,存储字符串并遍历
public class VectorDemo {
public static void main(String[] args) {
// 创建集合对象
Vector v = new Vector();
// 添加功能
v.addElement("hello");
v.addElement("world");
v.addElement("java");
// 遍历
for (int x = 0; x < v.size(); x++) {
String s = (String) v.elementAt(x);
System.out.println(s);
}
Enumeration en = v.elements(); // 返回的是实现类的对象
while (en.hasMoreElements()) {
String s = (String) en.nextElement();
System.out.println(s);
}
}
}
案例十一:LinkedList的使用,存储字符串并遍历
public class LinkedListDemo {
public static void main(String[] args) {
// 创建集合对象
LinkedList link = new LinkedList();
// 添加元素
link.addFirst("javaee");
link.addLast("android");
//获取元素
System.out.println("getFirst:" + link.getFirst());
System.out.println("getLast:" + link.getLast());
//删除元素
System.out.println("removeFirst:" + link.removeFirst());
System.out.println("removeLast:" + link.removeLast());
}
}