1.集合知识回顾
-
集合类的特点:提供了一种存储空间可变的存储模型,存储的数据容量可以随时发生改变。
-
集合类体系结构
2.Collection
集合概述和使用
-
Collection
集合概述- 是单例集合的顶层接口,它表示一组对象,这些对象也称为
Collection
的元素。 JDK
不提供此接口的任何直接实现,它提供更具体的子接口(如Set
和List
)实现
- 是单例集合的顶层接口,它表示一组对象,这些对象也称为
-
创建
Collection
集合的对象- 多态的方式
- 具体的实现类
ArrayList
-
代码示例
package study;
import java.util.ArrayList;
import java.util.Collection;
/*创建`Collection`集合的对象
- 多态的方式
- 具体的实现类`ArrayList`*/
public class CollectionDemo {
public static void main(String[] args) {
// 创建`Collection`集合的对象
Collection<String> c = new ArrayList<String>();
// 添加元素:boolean add (E e)
c.add("hello");
c.add("world");
c.add("Java");
// 输出集合对象
System.out.println(c);
}
}
3.Collection
集合常用方法
方法名 | 说明 |
---|---|
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 从集合中删除指定的元素 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否存在指定的元素 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 集合的长度,也就是集合中元素的个数 |
- 示例代码
package study;
import java.util.ArrayList;
import java.util.Collection;
// | `boolean add(E e)` | 添加元素
// | `boolean remove(Object o)` | 从集合中删除指定的元素
// | `void clear()` | 清空集合中的元素
// | `boolean contains(Object o)` | 判断集合中是否存在指定的元素
// | `boolean isEmpty()` | 判断集合是否为空
// | `int size()` | 集合的长度,也就是集合中元素的个数
//alt+7:能够打开一个窗口,看到一个类的所有信息
public class CollectionDemo1 {
public static void main(String[] args) {
// 创建集合对象
Collection<String> c = new ArrayList<String>();
// | `boolean add(E e)` | 添加元素
c.add("hello");
c.add("world");
c.add("java");
c.add("java1");
c.add("java");
System.out.println(c);
System.out.println("=======================");
// | `boolean remove(Object o)` | 从集合中删除指定的元素
c.remove("java");//只删除第一个符合条件的元素
System.out.println(c);
System.out.println("=======================");
// | `boolean contains(Object o)` | 判断集合中是否存在指定的元素
boolean i = c.contains("hello");
System.out.println(i);
System.out.println("=======================");
// | `boolean isEmpty()` | 判断集合是否为空
System.out.println("是否为空:" + c.isEmpty());
System.out.println("=======================");
// | `int size()` | 集合的长度,也就是集合中元素的个数
System.out.println(c.size());
System.out.println("=======================");
// | `void clear()` | 清空集合中的元素
c.clear();
System.out.println(c);
System.out.println("是否为空:" + c.isEmpty());
System.out.println("=======================");
}
}
4.Collection
集合的遍历
Iterator
:迭代器,集合的专用遍历方式
Iterator<E> iterator()
:返回此集合中元素的迭代器,通过集合的iterator()
方法得到。- 迭代器是通过集合的
iterator()
方法得到的,所以它是依赖于集合而存在的。
Iterator
中的常用方法
-
E next()
:返回迭代中的下一个元素 -
boolean hasNext()
:如果迭代具有更多元素,则返回true
。 -
代码示例
package study;
// `Iterator`:迭代器,集合的专用遍历方式
// - `Iterator<E> iterator()`:返回此集合中元素的迭代器,通过集合的`iterator()`方法得到。
// - 迭代器是通过集合的`iterator()`方法得到的,所以它是依赖于集合而存在的。
// `Iterator`中的常用方法
// - `E next()`:返回迭代中的下一个元素
// - `boolean hasNext()`:如果迭代具有更多元素,则返回`true`。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionDemo2 {
public static void main(String[] args) {
// 创建一个集合对象
Collection<String> c = new ArrayList<String>();
// 添加元素
c.add("hello");
c.add("world");
c.add("java");
// - `Iterator<E> iterator()`:返回此集合中元素的迭代器,通过集合的`iterator()`方法得到。
Iterator<String> it = c.iterator();
// `E next()`:返回迭代中的下一个元素
/* System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());//NoSuchElementException:请求被访问元素不存在*/
// - `boolean hasNext()`:如果迭代具有更多元素,则返回`true`。
/* if (it.hasNext()){
System.out.println(it.next());
}
if (it.hasNext()){
System.out.println(it.next());
}
if (it.hasNext()){
System.out.println(it.next());
}
if (it.hasNext()){
System.out.println(it.next());
}*/
// 改进
while (it.hasNext()){
// System.out.println(it.next());
String s = it.next();
System.out.println(s);
}
}
}
5.集合的使用步骤
案例
- Collection集合存储学生对象并遍历
需求:创建一个存储学生对象的集合,存储三个学生对象,使用程序在控制台遍历该集合。
- student类
package study01;
public class student {
private String name;
private int age;
private String sex;
//构造方法
public student(){}
public student(String name,String sex,int age){
this.age = age;
this.name = name;
this.sex = sex;
}
//set、get方法
public void setName(String name){
this.name = name;
}
public void setSex(String sex){
this.sex = sex;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public String getSex(){
return sex;
}
public int getAge(){
return age;
}
}
-
测试类
-
package study01; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class studentDemo { public static void main(String[] args) { //创建学生对象 student s1 = new student(); s1.setName("张三"); s1.setAge(15); s1.setSex("男"); student s2 = new student("李四","男",16); student s3 = new student("王二","女",17); //创建集合对象 Collection<student> c = new ArrayList<student>(); c.add(s1); c.add(s2); c.add(s3); //iterator Iterator<student> it = c.iterator(); //遍历学生对象 while (it.hasNext()){ student s4 = it.next(); studentOut(s4); } } private static void studentOut(student s){ System.out.println("姓名:"+s.getName()+"; 性别:"+s.getSex()+"; 年龄:"+s.getAge()); } }
s4 = it.next();
studentOut(s4);
}
}
private static void studentOut(student s){
System.out.println("姓名:"+s.getName()+"; 性别:"+s.getSex()+"; 年龄:"+s.getAge());
}
}
```