有勇气的牛排博客

java学习笔记 集合---Collection

有勇气的牛排 410 Java 2021-08-19 12:08:19

1 介绍

集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以随时发生变。

2 Collection

2.1 集合类体系结构

image.png

2.2 概述

软件包:java.util

  1. 是单列集合的顶层接口,他表示一组对象,这些对象也成为Collection的元素。

  2. JDK不提供此接口的任何直接实现,他提供更具体的子接口(如Set和List)实现。

2.3 创建Collection集合的对象

  • 多态的方式

  • 具体的实现类ArrayList

import java.util.ArrayList; import java.util.Collection; /** * 创建 Collection 集合的对象 * 多态的方式 * ArrayList() * */ public class CollectionDemo1 { 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); } }

输出:

[hello, world, java]

2.4 Collection集合常用方法

boolean add(E e): 添加元素

boolean remove(Object o): 从集合中移除指定的元素。

void clear:清空集合中的元素

boolean contains(Object o):判断集合中是否存在指定的元素

boolean isEmpty():判断集合是否为空

int size():集合的长度,也就是集合中元素的个数

import java.util.ArrayList; import java.util.Collection; /** * Collection 集合常用方法 * boolean add(E e): 添加元素 * boolean remove(Object o): 从集合中移除指定的元素。 * void clear:清空集合中的元素 * boolean contains(Object o):判断集合中是否存在指定的元素 * boolean isEmpty():判断集合是否为空 * int size()`:集合的长度,也就是集合中元素的个数 * */ public class CollectionDemo2 { public static void main(String[] args) { // 创建集合对象 Collection<String> c = new ArrayList<String>(); // boolean add(E e): 添加元素 System.out.println(c.add("hello")); System.out.println(c.add("world")); System.out.println(c.add("world")); // boolean remove(Object o): 从集合中移除指定的元素。 // 成功返回true 没有元素 返回flase System.out.println(c.remove("world")); // void clear:清空集合中的元素 // c.clear(); // boolean contains(Object o):判断集合中是否存在指定的元素 System.out.println(c.contains("hello")); // boolean isEmpty():判断集合是否为空 System.out.println(c.isEmpty()); // int size()`:集合的长度,也就是集合中元素的个数 System.out.println(c.size()); // 输出集合对象 System.out.println(c); } }

2.5 Collection集合的遍历

  • Iterator:迭代器,集合的专用遍历方式

Iterator<E> itertor():返回此集合中元素的迭代器,通过集合的iterator() 方法得到

迭代器是通过集合的iterator方法得到的,所以我们说它是依赖于集合而存在的

Iterator中常用的方法

  1. E next():返回迭代中的下一个元素

  2. boolean hashNext():如果迭代具有更多元素,则返回true

import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class ItertorDemo { public static void main(String[] args) { // 创建集合对象 Collection<String> c = new ArrayList<String>(); // 添加元素 c.add("hello"); c.add("world"); c.add("java"); // Iterator<E> itertor():返回此集合中元素的迭代器,通过集合的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()); // 元素不存在 异常 // 输出: // hello // world while (it.hasNext()){ String s = it.next(); System.out.println(s); } } }

2.6 Collection 集合存储学生对象并遍历

需求:创建一个存储学生对象的集合,存储3个学生对象,使用程序事项在控制台遍历该集合

思路

(1)定义学生类

(2)创建Collection集合对象

(3)创建学生对象

(4)把学生添加到集合

(5)遍历集合(迭代器方式)

学生类 Student.java

package CollectionDemo; 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 int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } }

CollectionDemo.java

package CollectionDemo; import java.util.ArrayDeque; import java.util.Collection; import java.util.Iterator; public class CollectionDemo { public static void main(String[] args) { // 创建 Collection 集合对象 Collection<Student> c = new ArrayDeque<Student>(); // 创建学生对象 Student s1 = new Student("灰太狼", 20); Student s2 = new Student("懒羊羊", 21); Student s3 = new Student("导演", 22); // 把学生添加到集合 c.add(s1); c.add(s2); c.add(s3); // 遍历集合(迭代器方式) Iterator<Student> it = c.iterator(); while (it.hasNext()) { Student s = it.next(); System.out.println(s.getName() + "," + s.getAge()); } } }

输出:

灰太狼,20 懒羊羊,21 导演,22

参考地址:
https://www.bilibili.com/video/BV18J411W7cE?p=221


留言

专栏
文章
加入群聊