1.对象数组
01.概念:数组既可以存储基本数据类型,也可以存储引用类型。它存储引用类型的时候的数组就叫对象数组。
eg:
package cn.itcast_01;
public class Student {
// 成员变量
private String name;
private int age;
// 构造方法
public Student() {
super();
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class ObjectArrayDemo {
public static void main(String[] args) {
// 创建学生数组(对象数组)。
Student[] students = new Student[5];
// for (int x = 0; x < students.length; x++) {
// System.out.println(students[x]);
// }
// System.out.println("---------------------");
// 创建5个学生对象,并赋值。
Student s1 = new Student("林", 27);
Student s2 = new Student("风", 30);
Student s3 = new Student("刘", 30);
Student s4 = new Student("赵", 60);
Student s5 = new Student("王", 35);
// 把C步骤的元素,放到数组中。
students[0] = s1;
students[1] = s2;
students[2] = s3;
students[3] = s4;
students[4] = s5;
// 遍历
for (int x = 0; x < students.length; x++) {
//System.out.println(students[x]);
Student s = students[x];
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
由上述代码可见,数组的长度固定,为适应变化的需求我们介绍下面集合。
2.集合Collection
01.Collection:是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的
import java.util.Collection;
数组和集合的区别?
A:长度区别
数组的长度固定
集合长度可变
B:内容不同
数组存储的是同一种类型的元素
而集合可以存储不同类型的元素
C:元素的数据类型问题
数组可以存储基本数据类型,也可以存储引用数据类型
集合只能存储引用类型
02.Collection的功能
Collection的功能概述:
1:添加功能
boolean add(Object obj):添加一个元素
boolean addAll(Collection c):添加一个集合的元素
2:删除功能
void clear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
3:判断功能
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
boolean isEmpty():判断集合是否为空
4:获取功能
Iterator<E> iterator()(重点)
5:长度功能
int size():元素的个数
面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
6:交集功能
boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
7:把集合转换为数组
Object[] toArray()
eg:
public class CollectionDemo {
public static void main(String[] args) {
// 测试不带All的方法
// 创建集合对象
// Collection c = new Collection(); //错误,因为接口不能实例化
Collection c = new ArrayList();
// boolean add(Object obj):添加一个元素
// System.out.println("add:"+c.add("hello"));
c.add("hello");
c.add("world");
c.add("java");
// void clear():移除所有元素
// c.clear();
// boolean remove(Object o):移除一个元素
// System.out.println("remove:" + c.remove("hello"));
// System.out.println("remove:" + c.remove("javaee"));
// boolean contains(Object o):判断集合中是否包含指定的元素
// System.out.println("contains:"+c.contains("hello"));
// System.out.println("contains:"+c.contains("android"));
// boolean isEmpty():判断集合是否为空
// System.out.println("isEmpty:"+c.isEmpty());
//int size():元素的个数
System.out.println("size:"+c.size());
System.out.println("c:" + c);
}
}
public class CollectionDemo2 {
public static void main(String[] args) {
// 创建集合1
Collection c1 = new ArrayList();
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c1.add("abc4");
// 创建集合2
Collection c2 = new ArrayList();
// c2.add("abc1");
// c2.add("abc2");
// c2.add("abc3");
// c2.add("abc4");
c2.add("abc5");
c2.add("abc6");
c2.add("abc7");
// boolean addAll(Collection c):添加一个集合的元素
// System.out.println("addAll:" + c1.addAll(c2));
//boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
//只要有一个元素被移除了,就返回true。
//System.out.println("removeAll:"+c1.removeAll(c2));
//boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
//只有包含所有的元素,才叫包含
// System.out.println("containsAll:"+c1.containsAll(c2));
//boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
//假设有两个集合A,B。
//A对B做交集,最终的结果保存在A中,B不变。
//返回值表示的是A是否发生过改变。
System.out.println("retainAll:"+c1.retainAll(c2));
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
}
}
public class CollectionDemo3 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 添加元素
c.add("hello"); // Object obj = "hello"; 向上转型
c.add("world");
c.add("java");
// 遍历
// Object[] toArray():把集合转成数组,可以实现集合的遍历
Object[] objs = c.toArray();
for (int x = 0; x < objs.length; x++) {
// System.out.println(objs[x]);
// 我知道元素是字符串,我在获取到元素的的同时,还想知道元素的长度。
// System.out.println(objs[x] + "---" + objs[x].length());
// 上面的实现不了,原因是Object中没有length()方法
// 我们要想使用字符串的方法,就必须把元素还原成字符串
// 向下转型
String s = (String) objs[x];
System.out.println(s + "---" + s.length());
}
}
}
03.练习
练习:用集合存储5个学生对象,并把学生对象进行遍历。
ublic class StudentDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
// 把学生添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 把集合转成数组
Object[] objs = c.toArray();
// 遍历数组
for (int x = 0; x < objs.length; x++) {
// System.out.println(objs[x]);
Student s = (Student) objs[x];
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
04.Iterator iterator():迭代器
Iterator iterator():迭代器,集合的专用遍历方式
Object next():获取元素,并移动到下一个位置。
NoSuchElementException:没有这样的元素,因为你已经找到最后了。
boolean hasNext():如果仍有元素可以迭代,则返回 true。
eg:
public class IteratorDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建并添加元素
// String s = "hello";
// c.add(s);
c.add("hello");
c.add("world");
c.add("java");
// Iterator iterator():迭代器,集合的专用遍历方式
Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态
while (it.hasNext()) {
// System.out.println(it.next());
String s = (String) it.next();
System.out.println(s);
}
}
}
练习:用集合存储5个学生对象,并把学生对象进行遍历。用迭代器遍历。
public class IteratorTest {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("林", 27);
Student s2 = new Student("风", 30);
Student s3 = new Student("令", 33);
Student s4 = new Student("武", 25);
Student s5 = new Student("刘", 22);
// 把学生添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 遍历
Iterator it = c.iterator();
while (it.hasNext()) {
// System.out.println(it.next());
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
需求:存储字符串并遍历。
public class CollectionTest {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建字符串对象
// 把字符串对象添加到集合中
c.add("林");
c.add("风");
c.add("刘");
c.add("武");
c.add("刘");
// 遍历集合
// 通过集合对象获取迭代器对象
Iterator it = c.iterator();
// 通过迭代器对象的hasNext()方法判断有没有元素
while (it.hasNext()) {
// 通过迭代器对象的next()方法获取元素
String s = (String) it.next();
System.out.println(s);
}
}
}
需求:存储自定义对象并遍历Student(name,age)
public class CollectionTest2 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("貂蝉", 25);
Student s2 = new Student("小乔", 16);
Student s3 = new Student("黄月英", 20);
Student s4 = new Student();
s4.setName("大乔");
s4.setAge(26);
// 把学生对象添加到集合对象中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(new Student("孙尚香", 18)); // 匿名对象
// 遍历集合
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
3. List集合
01.特点
有序(存储和取出的元素一致),可重复的。
因为List基础Collection接口,所以继承Collection用法。
public class ListDemo {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 创建字符串并添加字符串
list.add("hello");
list.add("world");
list.add("java");
// 遍历集合
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
public class ListDemo2 {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 存储元素
list.add("hello");
list.add("world");
list.add("java");
list.add("javaee");
list.add("android");
list.add("javaee");
list.add("android");
// 遍历集合
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
存储自定义对象并遍历
public class ListDemo {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 创建学生对象
Student s1 = new Student("白骨精", 30);
Student s2 = new Student("蜘蛛精", 40);
Student s3 = new Student("观音姐姐", 22);
// 把学生对象添加到集合对象中
list.add(s1);
list.add(s2);
list.add(s3);
// 遍历
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
02.List集合的特有功能:
A:添加功能
void add(int index,Object element):在指定位置添加元素
B:获取功能
Object get(int index):获取指定位置的元素
C:列表迭代器
ListIterator listIterator():List集合特有的迭代器
D:删除功能
Object remove(int index):根据索引删除元素,返回被删除的元素
E:修改功能
Object set(int index,Object element):根据索引修改元素,返回被修饰的元素
public class ListDemo {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// void add(int index,Object element):在指定位置添加元素
// list.add(1, "android");//没有问题
// IndexOutOfBoundsException
// list.add(11, "javaee");//有问题
// list.add(3, "javaee"); //没有问题
// list.add(4, "javaee"); //有问题
// Object get(int index):获取指定位置的元素
// System.out.println("get:" + list.get(1));
// IndexOutOfBoundsException
// System.out.println("get:" + list.get(11));
// Object remove(int index):根据索引删除元素,返回被删除的元素
// System.out.println("remove:" + list.remove(1));
// IndexOutOfBoundsException
// System.out.println("remove:" + list.remove(11));
// Object set(int index,Object element):根据索引修改元素,返回被修饰的元素
System.out.println("set:" + list.set(1, "javaee"));
System.out.println("list:" + list);
}
}
List集合的特有遍历功能:
size()和get()方法结合使用
public class ListDemo2 {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// Object get(int index):获取指定位置的元素
// System.out.println(list.get(0));
// System.out.println(list.get(1));
// System.out.println(list.get(2));
// IndexOutOfBoundsException
// System.out.println(list.get(3));
// 用循环改进
// for (int x = 0; x < 3; x++) {
// System.out.println(list.get(x));
// }
// 如果元素过多,数起来就比较麻烦,所以我们使用集合的一个长度功能:size()
// 最终的遍历方式就是:size()和get()
for (int x = 0; x < list.size(); x++) {
// System.out.println(list.get(x));
String s = (String) list.get(x);
System.out.println(s);
}
}
}
存储自定义对象并遍历,用普通for循环。(size()和get()结合)
public class ListDemo3 {
public static void main(String[] args) {
// 创建集合对象
List list = new ArrayList();
// 创建学生对象
Student s1 = new Student("林", 18);
Student s2 = new Student("刘", 88);
Student s3 = new Student("王", 38);
// 把学生添加到集合中
list.add(s1);
list.add(s2);
list.add(s3);
// 遍历
// 迭代器遍历
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("--------");
// 普通for循环
for (int x = 0; x < list.size(); x++) {
Student s = (Student) list.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
04.列表迭代器:
ListIterator listIterator():List集合特有的迭代器
该迭代器继承了Iterator迭代器,所以,就可以直接使用hasNext()和next()方法。
特有功能:
Object previous():获取上一个元素
boolean hasPrevious():判断是否有元素
注意:ListIterator可以实现逆向遍历,但是必须先正向遍历,才能逆向遍历,所以一般无意义,不使用。
public class ListIteratorDemo {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
// ListIterator listIterator()
ListIterator lit = list.listIterator(); // 子类对象
// while (lit.hasNext()) {
// String s = (String) lit.next();
// System.out.println(s);
// }
// System.out.println("-----------------");
// System.out.println(lit.previous());
// System.out.println(lit.previous());
// System.out.println(lit.previous());
// NoSuchElementException
// System.out.println(lit.previous());
while (lit.hasPrevious()) {
String s = (String) lit.previous();
System.out.println(s);
}
System.out.println("-----------------");
// 迭代器
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
System.out.println("-----------------");
}
}
/*
* 问题?
* 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现。
*
* ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
* 产生的原因:
* 迭代器是依赖于集合而存在的,在判断成功后,集合的中新添加了元素,而迭代器却不知道,所以就报错了,这个错叫并发修改异常。
* 其实这个问题描述的是:迭代器遍历元素的时候,通过集合是不能修改元素的。
* 如何解决呢?
* A:迭代器迭代元素,迭代器修改元素
* 元素是跟在刚才迭代的元素后面的。
* B:集合遍历元素,集合修改元素(普通for)
* 元素在最后添加的。
*/
public class ListIteratorDemo2 {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// 迭代器遍历
// Iterator it = list.iterator();
// while (it.hasNext()) {
// String s = (String) it.next();
// if ("world".equals(s)) {
// list.add("javaee");
// }
// }
// 方式1:迭代器迭代元素,迭代器修改元素
// 而Iterator迭代器却没有添加功能,所以我们使用其子接口ListIterator
// ListIterator lit = list.listIterator();
// while (lit.hasNext()) {
// String s = (String) lit.next();
// if ("world".equals(s)) {
// lit.add("javaee");
// }
// }
// 方式2:集合遍历元素,集合修改元素(普通for)
for (int x = 0; x < list.size(); x++) {
String s = (String) list.get(x);
if ("world".equals(s)) {
list.add("javaee");
}
}
System.out.println("list:" + list);
}
}