Java之集合的顶层父接口: Collection,list子接口:Arraylist、Linkedlist、Vector的使用
对象数组的概述和使用
案例:
需求:我有3个学生,请把这个3个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
学生:Student
成员变量:name,age
构造方法:无参,带参
成员方法:getXxx()/setXxx()
存储学生的数组?自己想想应该是什么样子的?
public class Student {
private String name;
private int age;
public Student() {
}
public Student(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
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class MyTest {
public static void main(String[] args) {
// 需求:我有3个学生,请把这个3个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("王祖贤", 20);
Student s3 = new Student("周慧敏", 30);
//定义数组
Student[] students = {s1, s2, s3, new Student("杨超越", 34)};
Object[] objects = {"str", Integer.valueOf(10), 100};
//遍历输出
for (int i = 0; i < students.length; i++) {
Student student = students[i];
System.out.println(student.getName() + "==" + student.getAge());
}
}
}
注意:
//数组作为容器,对容器中的元素的操作不是很方便,所以Java为了我们更方便的去操作容器中的元素,给我们提供了另一种容器,叫做集合
集合和数组的区别
- 数组的长度是固定的,集合的长度是可变的
- 数组可以存储基本数据类型,也可以存储引用数据类型,集合只能存储引用数据类型
- 数组只能存储同一种数据类型,集合可以存储多种数据类型
集合的由来及集合继承体系图
A:集合的由来
面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。
B:数组和集合的区别
(1): 长度区别:
数组的长度是固定的而集合的长度是可变的
(2): 存储数据类型的区别:
数组可以存储基本数据类型 , 也可以存储引用数据类型; 而集合只能存储引用数据类型
(3): 内容区别:
数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素
C:集合继承体系图
案例一:
import java.util.ArrayList;
import java.util.Collection;
public class MyTest {
public static void main(String[] args) {
//多态
Collection collection = new ArrayList();
//往容器中添加元素
collection.add("陈德容");
collection.add("黎姿");
collection.add("梁咏琪"); //虫虫
System.out.println(collection);//[陈德容, 黎姿, 梁咏琪]
}
}
Collection集合的功能概述
A:Collection的功能概述(通过API查看即可得到)
a:添加功能
boolean add(Object obj):添加一个元素
boolean addAll(Collection c):添加一个集合的元素 (给一个集合添加进另一个集合中的所有元素)
b:删除功能
void clear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):移除一个集合的元素(移除一个以上返回的就是true) 删除的元素是两个集合的交集元素
如果没有交集元素 则删除失败 返回false
c:判断功能
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(这个集合 包含 另一个集合中所有的元素才算包含 才返回true)
比如:1,2,3 containsAll 12=true 1,2,3 containsAll 2,3,4=false
boolean isEmpty():判断集合是否为空
d:获取功能
Iterator iterator()(重点)
e:长度功能
int size():元素的个数
面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
f:交集功能
//例如:A集合对B集合取交集,获取到的交集元素在A集合中。返回的布尔值表示的是A集合是否发生变化
boolean retainAll(Collection c):获取两个集合的交集元素(交集:两个集合都有的元素)
g:把集合转换为数组
Object[] toArray()
案例一:
import java.util.ArrayList;
import java.util.Collection;
public class MyTest2 {
public static void main(String[] args) {
Collection collection=new ArrayList();
collection.add(100);
collection.add(Integer.valueOf(103));
collection.add(100);
collection.add(Integer.valueOf(104));
collection.add(100);
collection.add(Integer.valueOf(104));
collection.add(100);
collection.add(Integer.valueOf(1022));
collection.add(100);
collection.add(Integer.valueOf(103));
Collection collection2 = new ArrayList();
collection2.add(100);
collection2.add(Integer.valueOf(103));
collection2.add(100);
collection2.add(Integer.valueOf(104));
collection2.add(100);
collection2.add(Integer.valueOf(104));
collection2.add(100);
collection2.add(Integer.valueOf(1022));
collection2.add(100);
collection2.add(Integer.valueOf(103));
//A 集合去addAll(B集合) 把B集合的所有元素放到A集合里面去
boolean b = collection.addAll(collection2);
System.out.println(collection);
System.out.println(collection2);
}
}
结果:
[100, 103, 100, 104, 100, 104, 100, 1022, 100, 103, 100, 103, 100, 104, 100, 104, 100, 1022, 100, 103]
[100, 103, 100, 104, 100, 104, 100, 1022, 100, 103]
案例二:
import java.util.ArrayList;
import java.util.Collection;
public class MyTest3 {
public static void main(String[] args) {
Collection collection = new ArrayList();
collection.add(10009);
collection.add(Integer.valueOf(103));
collection.add(100);
collection.add(Integer.valueOf(104));
collection.add(100);
collection.add(Integer.valueOf(104));
collection.add(100);
collection.add(Integer.valueOf(1022));
collection.add(100);
collection.add(Integer.valueOf(103));
//boolean remove = collection.remove(10009); //返回值是否删除成功
collection.clear();//清空集合中的所有元素
System.out.println(collection);
}
}
案例三:
import java.util.ArrayList;
import java.util.Collection;
public class MyTest4 {
public static void main(String[] args) {
//boolean removeAll (Collection c):移除一个集合的元素(移除一个以上返回的就是true) 删除的元素是两个集合的交集元素
//如果没有交集元素 则删除失败 返回false
//多态
Collection collection = new ArrayList();
//往容器中添加元素
collection.add("陈德容");
collection.add("黎姿");
collection.add("梁咏琪"); //虫虫
collection.add("张惠妹");
//多态
Collection collection2 = new ArrayList();
//往容器中添加元素
collection2.add("陈德容");
collection2.add("黎姿");
collection2.add("梁咏琪"); //虫虫
// A集合removeAll(B集合) A集合中,会删掉两个集合的交集元素,如果移除之后,A集合中发生变化返回true否则返回false
boolean b = collection.removeAll(collection2);
System.out.println(b);
System.out.println(collection);
System.out.println(collection2);
}
}
结果:true
[张惠妹]
[陈德容, 黎姿, 梁咏琪]
案例四:
import java.util.ArrayList;
import java.util.Collection;
public class MyTest5 {
public static void main(String[] args) {
//多态
Collection collection2 = new ArrayList();
//往容器中添加元素
collection2.add("陈德容");
collection2.add("黎姿");
collection2.add("梁咏琪"); //虫虫
//判断集合中是否存在该元素
boolean b = collection2.contains("陈德容2");
System.out.println(b);
}
}
结果:false
案例五:
import java.util.ArrayList;
import java.util.Collection;
public class MyTest6 {
public static void main(String[] args) {
Collection collection1 = new ArrayList();
//往容器中添加元素
collection1.add("陈德容");
collection1.add("黎姿");
collection1.add("梁咏琪"); //虫虫
//多态
Collection collection2 = new ArrayList();
//往容器中添加元素
collection2.add("陈德容");
collection2.add("黎姿");
collection2.add("梁咏琪2"); //虫虫
//A集合 containsAll(B集合) 判断B集合中的所有元素,是否都能够在A集合中找到
boolean b = collection1.containsAll(collection2);
System.out.println(b);
//boolean empty = "".isEmpty();//判断是否是空串
//判断集合中是否有元素
// collection2.clear();
boolean b1 = collection2.isEmpty();
System.out.println(b1);
}
}
结果:
false
false
案例六:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class MyTest {
public static void main(String[] args) {
Collection collection1 = new ArrayList();
//往容器中添加元素
collection1.add("陈德容");
collection1.add("黎姿");
collection1.add("梁咏琪"); //虫虫
//获取一个迭代器
//Iterator 对 collection 进行迭代的迭代器。
//boolean hasNext ()
//如果仍有元素可以迭代,则返回 true。
//E next ()
//返回迭代的下一个元素。
Iterator iterator = collection1.iterator();
System.out.println(iterator);
while (iterator.hasNext()){
Object obj = iterator.next();//让指针下移
System.out.println(obj);
}
}
}
结果:
[email protected]
陈德容
黎姿
梁咏琪
注意:
java.util.ArrayList$Itr @ 1540e19d 这是内部类编译过后的class文件名ArrayList$Itr
内部类:可以直接访问外部类的成员,包括私有的
案例七:
import com.sun.media.sound.SoftTuning;
import java.util.ArrayList;
import java.util.Collection;
public class MyTest2 {
public static void main(String[] args) {
Collection collection1 = new ArrayList();
//往容器中添加元素
collection1.add("陈德容");
collection1.add("黎姿");
collection1.add("梁咏琪"); //虫虫
int size = collection1.size();//获取集合的长度
System.out.println(size);//3
}
}
案例八:
public class Student {
private String name;
private int age;
public Student() {
}
public Student(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
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class MyTest {
public static void main(String[] args) {
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("王祖贤", 20);
Student s3 = new Student("周慧敏", 30);
Collection collection=new ArrayList();
collection.add(s1);
collection.add(s2);
collection.add(s3);
collection.add(new Student("张曼玉",24));
//遍历
Iterator iterator = collection.iterator();
// iterator.next();//手动移动了一下指针
while (iterator.hasNext()){
Object obj = iterator.next();
//向下转型
Student student= (Student) obj;
System.out.println(student.getName()+"==="+student.getAge());
}
}
}
结果:
林青霞===30
王祖贤===20
周慧敏===30
张曼玉===24
案例九:
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collection;
public class MyTest {
public static void main(String[] args) {
Collection collection=new ArrayList();
collection.add(10);
collection.add(20);
collection.add(30);
collection.add(40);
collection.add(60);
collection.add(50);
collection.add(60);
Collection collection2 = new ArrayList();
collection2.add(10);
collection2.add(20);
collection2.add(30);
collection2.add(40);
collection2.add(60);
//collection2.add(50);
//collection2.add(60);
//A集合retainAll(B集合); A集合放的是交集元素,A集合发生过变化返回true ,没发生过变化返回false
boolean b = collection.retainAll(collection2);
System.out.println(b);
System.out.println(collection);
System.out.println(collection2);
}
}
结果:
true
[10, 20, 30, 40, 60, 60]
[10, 20, 30, 40, 60]
案例十:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class MyTest2 {
public static void main(String[] args) {
Collection collection = new ArrayList();
collection.add(10);
collection.add(20);
collection.add(30);
collection.add(40);
collection.add(60);
collection.add(50);
collection.add(60);
////把集合转换成数组
//Integer[] integer=new Integer[collection.size()];
////遍历集合
//Iterator iterator = collection.iterator();
//int index=0;
//while (iterator.hasNext()){
// Object obj = iterator.next();
// Integer integer1= (Integer) obj;
// integer[index]=integer1;
// index++;
//}
//
//System.out.println(Arrays.toString(integer));
//把集合转换成数组
Object[] objects = collection.toArray();
Object object = objects[0];
Integer integer = (Integer) object;
System.out.println(integer + 100);//110
}
}
List概述及特点以及存储字符串并遍历
A:List概述及特点: 元素有序,并且每一个元素都存在一个索引.元素可以重复.
B:案例演示
List集合存储字符串并遍历
迭代器的方式
void add ( int index, E element)
List集合的特有功能概述和测试
A:List集合的特有功能概述
void add(int index,E element): 在指定索引处添加元素
E remove(int index):移除指定索引处的元素 返回的是移除的元素
E get(int index):获取指定索引处的元素
E set(int index,E element):更改指定索引处的元素 返回的而是被替换的元素
List集合的特有遍历功能
使用for循环 通过size()和get()方法结合使用遍历。
案例一:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MyTest {
public static void main(String[] args) {
//List 集合的特点:元素有序(存取顺序一致),允许重复元素
List list=new ArrayList();
list.add(100);
list.add(200);
list.add(300);
list.add(400);
list.add(0,10); //可以在指定索引处插入元素
//void add ( int index, E element)
//在列表的指定位置插入指定元素(可选操作)。
//
//Iterator iterator = list.iterator();
//while (iterator.hasNext()){
// System.out.println(iterator.next());
//}
//list.remove(0);//删掉指定索引处的元素
//list.remove(Integer.valueOf(100));//根据对象删除这个元素
//Iterator iterator = list.iterator();
//while (iterator.hasNext()){
// System.out.println(iterator.next());
//}
//
Object o = list.get(0);//获取集合中指定索引处的元素
System.out.println(o);
//遍历方式2
for (int i = 0; i < list.size(); i++) {
Object o1 = list.get(i);
System.out.println(o1);
}
}
}
结果:
10
10
100
200
300
400
ListIterator的特有功能
istIterator 继承自Iterator 可以使用Iterator中的方法
A:ListIterator的特有功能
boolean hasPrevious(): 是否存在前一个元素
E previous(): 返回列表中的前一个元素
以上两个方法可以实现反向遍历 但是注意 要完成反向遍历之前 要先进行正向遍历 这样指针才能移到最后
如果直接反向遍历是没有效果的 因为指针默认位置就在最前面 他前面没有元素
并发修改异常(ConcurrentModificationException)产生的原因及解决方案
A问题:我们用Iterator这个迭代器遍历采用hasNext方法和next方法,集合修改集合 会出现并发修改异常
B原因是我们的迭代依赖与集合 当我们往集合中添加好了元素之后 获取迭代器 那么迭代器已经知道了集合的元素个数
这个时候你在遍历的时候又突然想给 集合里面加一个元素(用的是集合的add方法) 那迭代器不同意 就报错了
C:解决方案 我们用ListIterator迭代器遍历 用迭代器自带的add方法添加元素 那就不会报错了
a:迭代器迭代元素,迭代器修改元素(ListIterator的特有功能add)
b:集合遍历元素,集合修改元素
解决方案2 使用for循环遍历集合 添加元素 不会报错
案例二:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class MyTest2 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(100);
list.add(200);
list.add(300);
list.add(400);
list.add(0, 10); //可以在指定索引处插入元素
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
System.out.println("---------------------");
//反向遍历 ,用同一个迭代器,先正向遍历完,才能反向遍历,直接反向遍历没效果,因为指针还在起始位置
//ListIterator listIterator1 = list.listIterator();
while (listIterator.hasPrevious()) {
Object previous = listIterator.previous();
System.out.println(previous);
}
}
}
结果:
10
100
200
300
400
400
300
200
100
10
注意:反向遍历 ,用同一个迭代器,先正向遍历完,才能反向遍历,直接反向遍历没效果,因为指针还在起始位置
案例三:
import java.util.ArrayList;
import java.util.List;
public class MyTest3 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(100);
list.add(200);
list.add(300);
list.add(400);
list.add(0, 10); //可以在指定索引处插入元素
Object set = list.set(list.size() - 1, 300000); //替换指定索引处的元素,返回的是被替换的元素
System.out.println(set);
for (int i = 0; i < list.size(); i++) {
Object o = list.get(i);
System.out.println(o);
}
}
}
结果:
400
10
100
200
300
300000
数据结构之栈和队列
A:数据结构概述及常见数据结构
数据结构其实就是存储数据的格式
分类: 栈 , 队列 , 数组 , 链表 , 树 , 哈希表
B:栈特点: 先进后出
C:队列: 先进先出
数据结构之数组和链表
A:数组特点: 查询快 , 增删慢
B:链表特点: 查询慢 , 增删快
List的三个子类的特点
A:List的三个子类的特点
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList:
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
B:List有三个儿子,我们到底使用谁呢?
得看 要安全还是要效率
是查找多还是增删多