2022.4.25集合 Collection

集合

什么是集合

对象的容器,实现了对对象常用的操作,类似数组功能

和数组的区别

  1. 数组长度固定,集合长度不固定

  2. 数组可以存储基本类型和引用类型,集合只能存储引用类型,基本类型通过自动装箱才可以被保存

位置

 java.util.*;

Collection体系

 

 

Collection 接口

特点:代表一组任意类型的对象,无序、无下标、不能重复。

Collection层次结构中的根接口。Collection表示一组对象,这些对象也称为collection的元素。一些collection允许有重复的元素,而另一些则不允许。一些collection是有序的,而另一些则是无序的。

常用方法

复制代码
1  boolean add(0bject obj)//添加一个对象。
2  boolean addAll(Collection c)//将一个集合中的所有对象添加到此集合中。
3  void clear()//清空此集合中的所有对象。
4  boolean contains (Object o)//检查此集合中是否包含o对象
5  boolean equals(Object o)//比较此集合是否与指定对象相等。
6  boolean isEmpty()//判断此集合是否为空
7  boolean remove(Object o)//在此集合中移除o对象
8  int size() //返回此集合中的元素个数。
9  0bject[] toArray()//将此集合转换成数组。
复制代码

 

  • 遍历元素(重点)

    • 使用增强for(因为无下标)

    • 使用迭代器:迭代器专门用来遍历集合的一种方式

1  Iterator为一个接口,主要有三个方法:
2  boolean hasNext()//如果仍有元素可以迭代,则返回true。没有指针下移操作,只是判断是否存在下一个元素
3  next();//返回迭代的下一个元素。指针下移,返回该指针所指向的元素
4  void remove()//删除当前元素,删除当前指针所指向的元素,一般和next方法一起用,这时候的作用就是删除next方法返回的元素
5 6  当Iterator迭代一个容器的时候,如果此时有别的方法在更改Collection(容器)的内容,那么Iterator就会抛出ConcurrentModificationException  并发修改异常  

 
复制代码
 1  package com.xing.collection;
 2  3  import java.util.ArrayList;
 4  import java.util.Collection;
 5  import java.util.Iterator;
 6  7  public class Demo01 {
 8      public static void main(String[] args) {
 9          //由于Collection是接口 ,只能实例化下面的实现类  创建一个集合
10          Collection collection = new ArrayList();
11          collection.add("苹果");
12          collection.add("狮子");
13          collection.add("老师");
14 15          System.out.println(collection.size());//对象个数
16          System.out.println(collection);//调用其重写的toString
17 18  //        collection.remove("苹果");//移除
19  //        collection.clear();//清空
20 21          //遍历 1.增强for
22          for (Object o : collection) {
23              System.out.println(o);
24          }
25 26          System.out.println("===========");
27          //2.使用迭代器:专门用来遍历集合的一种方式  集合想获取一个迭代器可以使用 iterator() 方法
28          Iterator it = collection.iterator();//返回Iterator类型 
29          while (it.hasNext()) {
30              Object o = it.next();
31              System.out.println(o);
32              //在使用迭代过程中不能使用Collection的方法collection.add();
33          }
34          
35          System.out.println(collection.contains("西瓜"));//判断  这个集合中有没有 这个字符串
36          System.out.println(collection.isEmpty());//判断是否为空
37      }
38  }
复制代码

 


 

 

 

迭代器Iterator

 

 

  • 当创建完成指向某个集合或者容器的Iterator对象时,这时的指针其实指向的是第一个元素的上方,即指向一个 空

  • 当调用hasNext方法的时候,只是判断下一个元素的有无,并不移动指针

  • 当调用next方法的时候,向下移动指针,并且返回指针指向的元素,如果指针指向的内存中没有元素,会报异 常。 在next()方法里,使下标加一,然后返回初始下标对应的对象。

  • remove方法删除的元素是指针指向的元素。如果当前指针指向的内存中没有元素,那么会抛出异常。

 

复制代码
 1  package com.xing.collection;
 2  3  import java.util.ArrayList;
 4  import java.util.Collection;
 5  import java.util.Iterator;
 6  7  public class Demo02 {
 8      public static void main(String[] args) {
 9          Student s1 = new Student("张三",20);
10          Student s2 = new Student("李四",18);
11          Student s3 = new Student("王五",22);
12 13          Collection collection = new ArrayList();
14 15          //添加数据
16          collection.add(s1);
17          collection.add(s2);
18          collection.add(s3);
19          System.out.println(collection.size());//查看是否成功
20          System.out.println(collection);//默认调用toString
21          //删除数据
22          collection.remove(s1);//不是删除对象,是删除集合里对象的地址
23          //遍历
24          Iterator iterator = collection.iterator();
25          while (iterator.hasNext()) {
26              System.out.println(iterator.next());
27          }
28      }
29  }
30  class Student{
31      private String name;
32      private int age;
33 34      public Student() {
35      }
36 37      public Student(String name, int age) {
38          this.name = name;
39          this.age = age;
40      }
41 42      public String getName() {
43          return name;
44      }
45 46      public void setName(String name) {
47          this.name = name;
48      }
49 50      public int getAge() {
51          return age;
52      }
53 54      public void setAge(int age) {
55          this.age = age;
56      }
57 58      @Override
59      public String toString() {
60          return "Student{" +
61                  "name='" + name + '\'' +
62                  ", age=" + age +
63                  '}';
64      }
65  }
复制代码

 

 

 

 

 

 

 

posted @   暴躁C语言  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示