集合

it2022-05-05  137

集合框架

对象数组 集合 数据结构

对象数组的概述和使用

A:案例演示 需求:我有3个学生,请把这个3个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。 学生:Student 成员变量:name,age 构造方法:无参,带参 成员方法:getXxx()/setXxx() Student student1 = new Student("张三", 23); Student student2 = new Student("李四", 24); Student student3= new Student("王伟", 25); // 数组 //对象数组 Student[] students={student1,student2,student3}; Student student = students[0]; System.out.println(student.getName()); 学生类 public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { 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 + '}'; } }

集合的由来及集合继承体系图

A:集合的由来 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。 B:数组和集合的区别 (1): 长度区别: 数组的长度是固定的而集合的长度是可变的 (2): 存储数据类型的区别: 数组可以存储基本数据类型 , 也可以存储引用数据类型; 而集合只能存储引用数据类型 (3): 内容区别: 数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素 C:集合继承体系图

Collection集合的功能概述

A:Collection的功能概述(通过API查看即可得到) a:添加功能 boolean add(Object obj):添加一个元素 boolean addAll(Collection c):添加一个集合的元素 (给一个集合添加进另一个集合中的所有元素) b:删除功能 void clear():移除所有元素 boolean remove(Object o):移除一个元素 boolean removeAll(Collection c):移除一个集合的元素(移除一个以上返回的就是true) 删除的元素是两个集合的交集元素 如果没有交集元素 则删除失败 返回false c:判断功能 boolean contains(Object o):判断集合中是否包含指定的元素 boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(这个集合 包含 另一个集合中所有的元素才算包含 才返回true) 比如:1,2,3 containsAll 12=true 1,2,3 containsAll 2,3,4=false boolean isEmpty():判断集合是否为空 d:获取功能 Iterator<E> iterator()(重点) e:长度功能 int size():元素的个数 面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢? f:交集功能 //例如:A集合对B集合取交集,获取到的交集元素在A集合中。返回的布尔值表示的是A集合是否发生变化 boolean retainAll(Collection c):获取两个集合的交集元素(交集:两个集合都有的元素) g:把集合转换为数组 Object[] toArray()

Collection集合的基本功能测试

A:案例演示 boolean add(E e) boolean remove(Object o) void clear() boolean contains(Object o) boolean isEmpty() int size() 演示 public class MyTest { public static void main(String[] args) { Collection collection = new ArrayList(); collection.add(100); collection.add(200); collection.add(Integer.valueOf(1)); //删除集合中的元素 boolean b = collection.remove(100); //根据元素删除,返回值代表是否删除成功 if(b){ System.out.println(collection); } //清空集合中所有的元素 collection.clear(); System.out.println(collection); } } public static void main(String[] args) { Collection collection = new ArrayList(); collection.add("王祖贤"); collection.add("张曼玉"); collection.add("惠英红"); collection.add("张敏"); collection.add("杨紫琼"); collection.add("陈德容"); //判断集合中有没有该元素 boolean b = collection.contains("张曼玉"); System.out.println(b); //boolean af = "afasfdsafd".contains("af"); collection.clear(); //判断集合是否为空 boolean empty = collection.isEmpty(); System.out.println(empty); //获取集合的长度 int size = collection.size(); }

15.06_集合框架(Collection集合的高级功能测试)(掌握)

A:案例演示 boolean addAll(Collection c) public class MyTest2 { public static void main(String[] args) { Collection collection= new ArrayList(); collection.add("王祖贤"); collection.add("张曼玉"); collection.add("惠英红"); collection.add("张敏"); collection.add("杨紫琼"); collection.add("陈德容"); Collection collection2 = new ArrayList(); collection2.add("王祖贤"); collection2.add("张曼玉"); collection2.add("惠英红"); collection2.add("张敏"); collection2.add("李丽珍"); collection2.add("邱淑贞"); collection2.add("翁虹"); collection2.add("林青霞"); //将两个集合中的元素放到一个集合中去 boolean b = collection.addAll(collection2); System.out.println(b); } } boolean removeAll(Collection c) public class MyTest3 { public static void main(String[] args) { //boolean removeAll (Collection c):移除一个集合的元素(移除一个以上返回的就是true) 删除的元素是两个集合的交集元素 //如果没有交集元素 则删除失败 返回false Collection collection = new ArrayList(); collection.add("王祖贤"); collection.add("张曼玉"); collection.add("惠英红"); collection.add("张敏"); collection.add("杨紫琼"); collection.add("陈德容"); Collection collection2 = new ArrayList(); collection2.add("王祖贤"); collection2.add("张曼玉"); collection2.add("惠英红"); collection2.add("张敏"); collection2.add("李丽珍"); collection2.add("邱淑贞"); collection2.add("翁虹"); collection2.add("林青霞"); //A集合removeAll(B集合); A集合会移除两个集合的交集元素,返回true,如果没有交集元素,移除失败返 回false boolean b = collection.removeAll(collection2); System.out.println(b); System.out.println(collection); System.out.println(collection2); } boolean containsAll(Collection c) public static void main(String[] args) { //boolean containsAll (Collection c):判断集合中是否包含指定的集合元素(这个集合 包含 另一个集合 中所有的元素才算包含 才返回true) //比如:1,2,3 containsAll 12 = true 1, 2, 3 containsAll 2, 3, 4 = false Collection collection = new ArrayList(); collection.add("王祖贤"); collection.add("张曼玉"); collection.add("惠英红"); collection.add("张敏"); collection.add("杨紫琼"); collection.add("陈德容"); Collection collection2 = new ArrayList(); collection2.add("王祖贤"); collection2.add("张曼玉"); collection2.add("惠英红"); collection2.add("张敏"); collection2.add("李丽珍"); collection2.add("邱淑贞"); collection2.add("翁虹"); collection2.add("林青霞"); collection2.add("杨紫琼"); collection2.add("陈德容"); / /A集合containsAll(B集合) 如果说B集合中的元素,在A集合中都有出现,那么返回true 否则false boolean b = collection2.containsAll(collection); System.out.println(b); boolean retainAll(Collection c) public static void main(String[] args) { Collection collection = new ArrayList(); collection.add("王祖贤"); collection.add("张曼玉"); collection.add("惠英红"); collection.add("张敏"); collection.add("杨紫琼"); collection.add("陈德容"); collection.add("陈德容2"); Collection collection2 = new ArrayList(); collection2.add("王祖贤"); collection2.add("张曼玉"); collection2.add("惠英红"); collection2.add("张敏"); collection2.add("杨紫琼"); collection2.add("陈德容"); //取两个集合中的交集元素 // A集合retainAll(B集合);如果取到了交集元素,这些交集元素会放到A集合里面去,A集合中原有的非交集元素也会删除。 //返回值的意思是,取完交集后,A集合中有么有发生过变化 boolean b = collection.retainAll(collection2); System.out.println(b); System.out.println(collection); System.out.println(collection2); }

集合的遍历之集合转数组遍历

A:集合的遍历 toArray() 把一个集合转成数组 其实就是依次获取集合中的每一个元素。 B:案例演示 把集合转成数组toArray(),遍历这个数组 可以实现集合的遍历 public static void main(String[] args) { Collection collection = new ArrayList(); collection.add("王祖贤"); collection.add("张曼玉"); collection.add("惠英红"); collection.add("张敏"); collection.add("杨紫琼"); collection.add("陈德容"); //把集合中的元素,放到数组中 /* String[] strings = new String[collection.size()]; Iterator iterator = collection.iterator(); int index=0; while (iterator.hasNext()) { Object ele = iterator.next(); strings[index++]= (String) ele; } System.out.println(Arrays.toString(strings));*/ //把集合转成数组 Object[] objects = collection.toArray(); System.out.println(Arrays.toString(objects)); }

Collection存储自定义对象并遍历

public static void main(String[] args) { Collection collection = new ArrayList(); collection.add("王祖贤"); collection.add("张曼玉"); collection.add("惠英红"); collection.add("张敏"); collection.add("杨紫琼"); collection.add("陈德容"); //获取迭代器 // 对 collection 进行迭代的迭代器。 // boolean hasNext () // 如果仍有元素可以迭代,则返回 true。 // E next () // 返回迭代的下一个元素。 // void remove () // 从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。 //迭代器,用来遍历集合中的元素, Iterator iterator = collection.iterator(); System.out.println(iterator); // ArrayList$Itr while (iterator.hasNext()){ Object next = iterator.next(); System.out.println(next); } }

List集合的特有功能概述和测试

A:List集合的特有功能概述 void add(int index,E element): 在指定索引处添加元素 E remove(int index):移除指定索引处的元素 返回的是移除的元素 E get(int index):获取指定索引处的元素 E set(int index,E element):更改指定索引处的元素 返回的而是被替换的元素 B:案例演示: 特有功能测试 public static void main(String[] args) { //List:元素有序(存取元素顺序一致),允许重复元素 List list = new ArrayList(); //多态 list.add("林青霞"); //List里面特有的一个添加元素的方法,可以指定索引 list.add(0,"李亦非"); list.add(1,"张曼玉"); list.add("王祖贤"); //移除元素 list.remove("王祖贤"); list.remove(0); //List里面特有的方法,根据索引移除元素 System.out.println(list); } public static void main(String[] args) { List list = new ArrayList(); //多态 list.add("林青霞"); list.add("李亦非"); list.add("张曼玉"); list.add("王祖贤"); //List 特有的方法根据索引获取元素 Object o = list.get(2); //IndexOutOfBoundsException: 索引越界 System.out.println(o); //List 里面特有的方法,根据索引替换元素,返回值是被替换的旧元素 Object obj = list.set(2, "梅艳芳"); System.out.println(list); }

List集合的遍历功能

public static void main(String[] args) { List list = new ArrayList(); //多态 list.add("林青霞"); list.add("李亦非"); list.add("张曼玉"); list.add("王祖贤"); //遍历List集合的方式1 Iterator iterator = list.iterator(); while (iterator.hasNext()){ Object next = iterator.next(); System.out.println(next); } System.out.println("--------------------------------------"); //使用for循环来遍历List集合 for (int i = 0; i < list.size(); i++) { Object o = list.get(i); System.out.println(o); } } public static void main(String[] args) { //List 集合自己的一个迭代器 List list = new ArrayList(); list.add(new Student("张三", 23)); list.add(new Student("王五", 25)); list.add(new Student("赵六", 26)); ListIterator listIterator = list.listIterator(); //listIterator.next(); //手动迭代 //listIterator.next(); //listIterator.next(); while (listIterator.hasNext()){ Object next = listIterator.next(); System.out.println(next); } System.out.println("-----------------------"); // ListIterator 它里面有两个特有的方法,可以反向迭代 //我们进行反向迭代之前,先进行正向迭代 //ListIterator listIterator1 = list.listIterator(); //while (listIterator.hasPrevious()){ // Object previous = listIterator.previous(); // System.out.println(previous); //} }

ListIterator的特有功能

``` ListIterator 继承自Iterator 可以使用Iterator中的方法 A:ListIterator的特有功能 boolean hasPrevious(): 是否存在前一个元素 E previous(): 返回列表中的前一个元素 以上两个方法可以实现反向遍历 但是注意 要完成反向遍历之前 要先进行正向遍历 这样指针才能移到最后​ 如果直接反向遍历是没有效果的 因为指针默认位置就在最前面 他前面没有元素

15.20_集合框架(并发修改异常产生的原因及解决方案)(了解)

A:案例演示 需求:我有一个集合,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现。 B:ConcurrentModificationException出现 我们用Iterator这个迭代器遍历采用hasNext方法和next方法,集合修改集合 会出现并发修改异常 原因是我们的迭代依赖与集合 当我们往集合中添加好了元素之后 获取迭代器 那么迭代器已经知道了集合的元素个数 这个时候你在遍历的时候又突然想给 集合里面加一个元素(用的是集合的add方法) 那迭代器不同意 就报错了 C:解决方案 我们用ListIterator迭代器遍历 用迭代器自带的add方法添加元素 那就不会报错了 a:迭代器迭代元素,迭代器修改元素(ListIterator的特有功能add) b:集合遍历元素,集合修改元素 解决方案2 使用for循环遍历集合 添加元素 不会报错 public static void main(String[] args) { List list = new ArrayList(); list.add("hello"); list.add("world"); list.add("PHP"); list.add("Java"); //boolean b= list.contains("world"); //if(b){ // list.add("JavaEE"); //} // //System.out.println(list); //ConcurrentModificationException 并发修改异常 //当我们使用迭代器,遍历集合元素时,我们的迭代器事先已经知晓,集合中的元素个数, //你突然在迭代途中,想要增加或删除,一个元素,那么就会打乱,原来集合中的元素的顺序,那么迭代器也就无所适从了。就会抛出一个并发修改异常。 //解决方式1:我们使用迭代器进行遍历,迭代途中如果要添加元素和删除元素,可以使用迭代器的添加删除元素的方法 /* ListIterator listIterator = list.listIterator(); while (listIterator.hasNext()){ Object obj = listIterator.next(); String str= (String) obj; if(str.equals("world")){ //list.add("JavaEE"); //list.remove("Java"); //ConcurrentModificationException 并发修改异常 //迭代途中如果要添加元素和删除元素,可以使用迭代器的添加删除元素的方法 listIterator.add("JavaEE"); //listIterator.remove(); } }*/ //可以使用for循环来遍历,遍历途中你可以使用集合自己的添加和删除元素的方法 for (int i = 0; i < list.size(); i++) { Object obj = list.get(i); String str = (String) obj; if (str.equals("world")) { // list.add("JavaEE"); list.remove("Java"); } } System.out.println(list); }

数据结构之栈和队列

A:数据结构概述及常见数据结构 数据结构其实就是存储数据的格式 分类: 栈 , 队列 , 数组 , 链表 , 树 , 哈希表 B:栈特点: 先进后出 C:队列: 先进先出

数据结构之数组和链表

A:数组特点: 查询快 , 增删慢 B:链表特点: 查询慢 , 增删快

List的三个子类的特点

A:List的三个子类的特点 ArrayList: 底层数据结构是数组,查询快,增删慢。 线程不安全,效率高。 Vector: 底层数据结构是数组,查询快,增删慢。 线程安全,效率低。 LinkedList: 底层数据结构是链表,查询慢,增删快。 线程不安全,效率高。 B:List有三个儿子,我们到底使用谁呢? 得看 要安全还是要效率 是查找多还是增删多

最新回复(0)