Collection集合
Collection集合概述
是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素
JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现
创建Collection集合的对象
多态的方式
具体的实现类ArrayList
Collection集合的概述
是单列集合的顶层接口,它表示一组对象,这些对象也称为Collection元素
JDK不提供此接口的直接实现类,它提供更具体的子接口(Set和List)
Collection集合的基本使用
通过多态形式创建对象:Collection c = new ArrayList()
Collection集合的常用方法
boolean add(E e) 向集合中添加元素
boolean remove(E e) 将元素从集合中删除
void clear() 清空集合所有的元素
boolean contains(E e) 判断集合中是否包含指定的元素
boolean isEmpty() 判断集合是否为空
int size() 获取集合的长度
/*
Collection集合的常用方法
boolean add(E e) 向集合中添加元素
boolean remove(E e) 将元素从集合中删除
void clear() 清空集合所有的元素
boolean contains(E e) 判断集合中是否包含指定的元素
boolean isEmpty() 判断集合是否为空
int size() 获取集合的长度
*/
import java.util.ArrayList;
import java.util.Collection;
public class lianxi01 {
public static void main(String[] args) {
Collection<String> coll = new ArrayList<>();
//boolean add(E e) 向集合中添加元素
coll.add("早上好");
coll.add("下午好");
coll.add("晚上好");
System.out.println(coll);//[早上好, 下午好, 晚上好]
System.out.println("========");
//boolean remove(E e) 将元素从集合中删除
coll.remove("下午好");
System.out.println(coll);//[早上好, 晚上好]
System.out.println("========");
// void clear() 清空集合所有的元素
coll.clear();
System.out.println(coll);//[]
System.out.println("========");
//boolean contains(E e) 判断集合中是否包含指定的元素
coll.add("下午好");
System.out.println(coll.contains("早上好"));//false
System.out.println(coll.contains("下午好"));//true
System.out.println("=========");
// boolean isEmpty() 判断集合是否为空
System.out.println(coll.isEmpty());//true
System.out.println("=======");
// int size() 获取集合的长度
System.out.println(coll.size());//1
}
}
Collection集合的遍历
遍历Collection集合的重要步骤
1.通过集合对象获取迭代器对象
2.使用循环和hasNext()方法来控制是否还有元素
3.循环内通过next()方法获取元素
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
Collection集合的遍历
iterator 详情>
英 [ɪtə'reɪtə] 美 [ɪtə'reɪtə]
[计] 迭代器,迭代程序;
*/
public class lianxi02 {
public static void main(String[] args) {
Collection<String> coll = new ArrayList<>();
coll.add("早上好");
coll.add("下午好");
coll.add("晚上好");
//1.通过集合对象获取迭代器对象
Iterator<String> it = coll.iterator();
// 2.使用循环和hasNext()方法来控制是否还有元素
while (it.hasNext()) {
// 3.循环内通过next()方法获取元素
String s = it.next();
System.out.println(s);
// 早上好
// 下午好
// 晚上好
}
}
}
案例:
Collection集合存储学生对象并遍历的重要步骤
1.定义一个学生类,包含姓名和年龄两个属性
2.创建Collection集合对象,泛型为学生对象类型
3.创建几个学生对象,并存储到集合中
4.通过迭代器遍历集合
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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;
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionDemo {
public static void main(String[] args) {
Collection<Student> Collec = new ArrayList<>();
Collec.add(new Student("张三",55));
Collec.add(new Student("李四",77));
Collec.add(new Student("万二",44));
Iterator<Student> it = Collec.iterator();
while (it.hasNext()){
Student stu = it.next();
System.out.println(stu);
System.out.println(stu.getName()+","+stu.getAge());
// A01_Colletion.lianxi03.Student@3f3afe78
// 张三,55
// A01_Colletion.lianxi03.Student@64616ca2
// 李四,77
// A01_Colletion.lianxi03.Student@13fee20c
// 万二,44
}
}
List集合
List集合的概述和特点
1.List集合是单列集合体系之一
2.List集合及其实现类全部有索引
3.List集合及其实现类全部可以存储重复元素
4.List集合及其实现类全部元素存取有序
List集合特有的方法
void add(int index,E e) 向集合中指定索引位置添加元素
E remove(int index) 删除指定索引处的元素并返回
E set(int index,E e) 修改指定索引处的元素,返回被修改的元素
E get(int index) 获取指定索引处的元素
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class lianxi01 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("早上好");
list.add("中午好");
list.add("晚上好");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
// 早上好
// 中午好
// 晚上好
//void add(int index,E e) 向集合中指定索引位置添加元素
}
list.add(2, "吃了吗");
System.out.println(list);//[早上好, 中午好, 吃了吗, 晚上好]
//E remove(int index) 删除指定索引处的元素并返回
System.out.println(list.remove(2));//吃了吗
// E set(int index,E e) 修改指定索引处的元素,返回被修改的元素
System.out.println(list.set(2, "真热"));//晚上好
// E get(int index) 获取指定索引处的元素
System.out.println(list.get(2));//真热
System.out.println(list);//[早上好, 中午好, 真热]
}
}
练习
导包学生类
创建List集合对象,泛型为学生对象类型
创建几个学生对象,并添加到集合中
通过迭代器和for循环两种方式遍历集合
import A01_Colletion.lianxi03.Student;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
导包学生类
创建List集合对象,泛型为学生对象类型
创建几个学生对象,并添加到集合中
通过迭代器和for循环两种方式遍历集合
*/
public class StudentListDemo {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("张三", 55));
list.add(new Student("李四", 77));
list.add(new Student("万二", 44));
//通过迭代器
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName() + "," + list.get(i).getAge());
}
}
}
ArrayList集合
ArrayList集合的介绍
ArrayList集合是一个长度可变的容器
ArrayList集合的特点
ArrayList集合的长度是可变的
ArrayList集合底层是数组实现
例如:
我们想存储字符串类型的数据,就可以这样写:ArrayList
我们想存储学生类型的数据,就可以这样写:ArrayList
ArrayList集合的构造方法
public ArrayList() 创建一个空的集合容器
ArrayList常用方法
public boolean add(E e) 向集合中添加元素
public void add(int index,E e) 向集合中指定索引处添加元素
public boolean remove(Object obj) 删除集合中指定元素
public E remove(int index) 删除集合中指定索引处的元素
public E set(int index,E e) 修改集合中指定索引处的元素
public E get(int index) 获取集合中指定索引处的元素
public int size() 获取集合的长度
LinkedList集合
LinkedList集合的特有功能
void addFirst(E e) 向集合中第一个位置添加元素
void addLast(E e) 向集合中最后一个位置添加元素
E getFirst() 获取集合中第一个元素
E getLast() 获取集合中最后一个元素
E removeFirst() 删除集合中第一个元素
E removeLast() 删除集合中最后一个元素
import java.util.LinkedList;
/*
LinkedList
Linked
英 ['lɪŋkt] 美 ['lɪŋkt]
adj.
连接的,显示连环遗传的;
LinkedList集合的特有功能
void addFirst(E e) 向集合中第一个位置添加元素
void addLast(E e) 向集合中最后一个位置添加元素
E getFirst() 获取集合中第一个元素
E getLast() 获取集合中最后一个元素
E removeFirst() 删除集合中第一个元素
E removeLast() 删除集合中最后一个元素
*/
public class lainxi01 {
public static void main(String[] args) {
LinkedList<String> linked = new LinkedList<>();
linked.add("吃了吗");
//void addLast(E e) 向集合中最后一个位置添加元素
linked.addLast("晚上好");
//void addFirst(E e) 向集合中第一个位置添加元素
linked.addFirst("早上好");
// E getFirst() 获取集合中第一个元素
System.out.println(linked.getFirst());//早上好
// E getLast() 获取集合中最后一个元素
System.out.println(linked.getLast());//晚上好
// E removeFirst() 删除集合中第一个元素
System.out.println(linked);//[早上好, 吃了吗, 晚上好]
System.out.println("========");
linked.removeFirst();
System.out.println(linked);//[ 吃了吗, 晚上好]
// E removeLast() 删除集合中最后一个元素
linked.removeLast();
System.out.println(linked);//[吃了吗]
}
}
文章有不对之处,欢迎同学们指出修改。