集合
什么是集合
-
概念
对象的容器,实现了对对象常用的操作
-
和数组的区别
-
数组长度固定,集合长度不固定
-
数组可以存储基本类型和引用类型,集合只能存储引用类型
-
Collection体系
Collection父接口
特点:代表一组任意类型的对象,无序、无下标、不能重复。
创建集合 Collection collection = new ArrayList();
-
常用方法
-
添加元素
collection.add();
-
删除元素
collection.remove();
collection.clear();
-
遍历元素(重点)
-
使用增强for(因为无下标)
for(Object object : collection){ }
-
使用迭代器
//hasNext(); 有没有下一个元素
//next(); 获取下一个元素
//remove(); 删除当前元素
Iterator it = collection.iterator();
while(it.hasNext()){
String object = (String)it.next(); //强转
// 可以使用it.remove(); 进行移除元素
// collection.remove(); 不能用collection其他方法 会报并发修改异常
}
-
-
判断
collection.contains();
collection.isEmpty();
-
代码参考
package com.pc.zjs;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection接口的使用
*(1)添加元素
*(2)删除元素
*(3)遍历元素
*(4)判断
*/
public class Demo01 {
public static void main(String[] args) {
//创建集合
Collection collection=new ArrayList();
//(1)添加元素
collection.add("苹果");
collection.add("西瓜");
collection.add("榴莲");
System.out.println("元素个数"+collection.size());
System.out.println(collection);
//删除元素
//collection.remove("榴莲");
//collection.clear();删除全部
//System.out.println("删除之后"+collection.size());
//(3)遍历元素
//3.1增强for
System.out.println("------使用增强for-------");
for(Object object:collection){
System.out.println(object);
}
//3.2使用迭代器
/*
hasNext();有没有下一个元素
next();获取下一个元素
remove();删除当前元素
*/
System.out.println("------使用迭代器--------");
Iterator it=collection.iterator();
while (it.hasNext()){
String s = (String)it.next();
System.out.println(s);
//collection.remove(s);此处迭代器正在使用collection不允许对collection 进行操作
it.remove();
}
System.out.println("元素个数"+collection.size());
/*
(4)判断
*/
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());
}
}
package com.pc.zjs;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection的使用:保存学生信息
*/
public class Demo02 {
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("王二",22);
//1.添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数"+collection.size());
System.out.println(collection.toString());
//2.删除
//collection.remove(s1);
//collection.clear();
//System.out.println("删除之后"+collection.size());
//3.遍历
/*
3.1增强for
*/
System.out.println("----使用增强for----");
for(Object object:collection){
Student s=(Student)object;
System.out.println(s.toString());
}
//3.2迭代器
System.out.println("---使用迭代器---");
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());
}
}
package com.pc.zjs;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
//super();
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;
}
List子接口
特点:有序、有下标、元素可重复
创建集合对象 List list = new ArrayList<>( );
-
常用方法
-
添加元素
list.add( );
会对基本类型进行自动装箱 -
删除元素 可以用索引
list.remove(0)
当删除数字与索引矛盾时 对数字强转
list.remove((Object) 10)
或list.remove(new Integer(10))
-
遍历
-
使用for遍历
for(int i = 0; i < list.size(); i++){
sout(list.get(i));
} -
使用增强for
for(Object list: collection){ }
-
使用迭代器
Iterator it = collection.iterator();
while(it.hasNext()){
String object = (String)it.next(); //强转
// 可以使用it.remove(); 进行移除元素
// collection.remove(); 不能用collection其他方法 会报并发修改异常
} -
使用列表迭代器
-
-