| 1.集合的概念 | 2.Collection接口 | 3.List接口与实现类 |
|---|---|---|
| 4.泛型和工具类 | 5.Set接口与实现类 | 6.Map接口与实现类 |
集合:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。
和数组的区别:
(1)数组长度固定,集合长度不固定。
(2)数组可以存储基本类型和引用类型,集合只能存储引用类型。
位置:java.util.*;
特点:代表一组任意类型的对象,无序、无下标、不能重复。
方法:
boolean add(Object obj) //添加一个对象
boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中
void clear() //清空此集合中的所有对象
boolean contains(Object o) //检查此集合中是否包含o对象
boolean equals(Object o) //比较此集合是否与指定对象相等
boolean isEmpty() //判断此集合是否为空
boolean remove(Object o) //在此集合中移除o对象
int size() //返回此集合中的元素个数
Object[ ] toArray() //将此集合转换为数组
(1.)Collecction的使用:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo03 {
public static void main(String[] args) {
//创建集合
Collection collection = new ArrayList();
//Collection接口的使用
//1.添加元素
collection.add("苹果");
collection.add("西瓜");
collection.add("榴莲");
System.out.println("元素个数:"+collection.size());//元素个数:3
System.out.println(collection);//[苹果,西瓜,榴莲]
//2.删除元素
//3.遍历元素【重点】
//3.1增强for
for(Object object:collection){
System.out.println(object);
}//苹果/n西瓜/n榴莲/n
//3.2使用迭代器(迭代器专门用来遍历集合的一种方式)
//hasNext();有没有下一个元素
//next();获取下一个元素
//remove();删除当前元素
Iterator it = collection.iterator();
while (it.hasNext()){
String s = (String)it.next();
System.out.println(s);//苹果/n西瓜/n榴莲/n
it.remove();
}
System.out.println(collection.size());//0
//4.判断
System.out.println(collection.contains("西瓜"));//false
System.out.println(collection.isEmpty());//ture
}
}
(2.)Collection的使用:保存学生信息
public class Student {//学生类
private String name;
private int 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 Student(String name, int age){
super();
this.age=age;
this.name=name;
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo03 {
public static void main(String[] args) {
//新建Collection对象
Collection collection = new ArrayList();
Student s1 = new Student("张三",20);
Student s2 = new Student("李四",18);
Student s3 = new Student("王五",28);
//1添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数:"+collection.size());
System.out.println(collection.toString());
//2.删除
collection.remove(s1);
System.out.println("删除之后:"+collection.size());
//遍历3.1增强for
for (Object object:collection) {
Student s = (Student)object;
System.out.println(s.toString());
}
//3.2迭代器:hasNext() next() remove();迭代过程中
//不能使用collection的删除方法
Iterator it=collection.iterator();
while(it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
//4.判断
System.out.println(collection.contains(s1));
System.out.println(collection.isEmpty());// 判断是否为空
}
}
元素个数:3
[Student{name=‘张三’, age=20}, Student{name=‘李四’, age=18}, Student{name=‘王五’, age=28}]
删除之后:2
Student{name=‘李四’, age=18}
Student{name=‘王五’, age=28}
Student{name=‘李四’, age=18}
Student{name=‘王五’, age=28}
false
(1.)List子接口
特点:有序、有下标、元素可以重复.
方法:
-void add(int index,Object o) //在index位置插入对象o.
-boolean addAll(int index,Collection c) //将一个集合中的元素添加到此集合中的index位置.
-Object get(int index) //返回集合中指定位置的元素.
-List subList(int fromIndex,int toIndex) //返回fromIndex和toIndex之间的集合元素.
List子接口的使用①:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Demo04 {
public static void main(String[] args) {
//先创建集合对象
List list = new ArrayList();
//1.添加元素
list.add("苹果");
list.add("小米");
list.add(0,"华为");
System.out.println("元素个数为:"+list.size());//元素个数为:3
System.out.println(list.toString());//[华为,苹果,小米]
//2.删除元素
list.remove(0);
System.out.println("删除之后:"+list.size());//删除之后:2
System.out.println(list.toString());//[苹果,小米]
//3.遍历
//3.1使用for遍历
for (int i=0;i< list.size();i++){//苹果 /n 小米
System.out.println(list.get(i));
}//3.2增强for
for (Object object: list) {
System.out.println(object);
}//3.3使用迭代器
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}//3.4列表迭代器,和Iterator的区别,
// ListIterator可以向前或向后遍历、添加、删除、修改元素
System.out.println("----------从前向后遍历----------");
ListIterator lit =list.listIterator();
while(lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}//0:苹果 /n 1:小米
System.out.println("----------从后向前遍历----------");
while(lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}//1:小米 /n 0:苹果
//判断
System.out.println(list.contains("苹果"));//true
System.out.println(list.isEmpty());//false
//获取位置
System.out.println(list.indexOf("小米"));//1
}
}
List接口使用②:
import java.util.ArrayList;
import java.util.List;
public class Demo05 {
public static void main(String[] args) {
//创建集合
List list = new ArrayList();
//1.添加数字数据(自动装箱)
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//2.删除操作
list.remove(4);//根据下标来删除
System.out.println("删除元素:"+list.size());
System.out.println(list.toString());
//3.补充方法subList,返回子集合,含头不含尾
List subList = list.subList(1,3);
System.out.println(subList.toString());
}
}
元素个数:5
[20, 30, 40, 50, 60]
删除元素:4
[20, 30, 40, 50]
[30, 40]



