【JavaEE】Day05-Collection集合&迭代器

###1.Collection集合

1.1 集合体系结构

* 集合类的特点
	提供一种存储空间可变的存储类型,存储的数据类型可以随时发生改变

Collection集合(接口)
* 是单列集合的顶层接口
* 必须使用子类来实现
Collection集合基本使用

public class CollectionDemo01 {
    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);
    }
}

###1.2 单列集合
  Collection(接口)
  List(接口)
  	特点:1.可以存储重复的元素
  		  2.有索引
  	* 不能直接用,要用实现类ArrayList和
  Set(接口)
  	特点:1.不可以存储重复的元素
  		  2.没有索引
  
  ### 1.3Collection集合的常用方法【应用】

| 方法名                     | 说明                               |
| -------------------------- | ---------------------------------- |
| boolean add(E e)           | 添加元素                           |
| boolean remove(Object o)   | 从集合中移除指定的元素             |
| void   clear()             | 清空集合中的元素                   |
| boolean contains(Object o) | 判断集合中是否存在指定的元素       |
| boolean isEmpty()          | 判断集合是否为空                   |
| int   size()               | 集合的长度,也就是集合中元素的个数 |
  
### 1.4Collection集合的遍历【应用】

- 迭代器的介绍
- 迭代器,集合的专用遍历方式
- Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
- 迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
  *-为什么学习迭代器?
  		因为我们使用普通的for循环只能遍历有索引的集合,不能遍历没有索引的集合。
  		所有普通的for循环不是通用的遍历格式,而迭代器就是一种通用的遍历格式
  迭代器的使用步骤:
  		1.获取迭代器
  			集合对象.iterator();
  		2.通过迭代器对象的方法来去获取集合中的元素
  			hasNext();  ------->判断有没有下一个元素
  			next();    --------> 取出下一个元素
  	
*查询字符串中包含某些元素 用contains来检测

例如:
  需求:找出集合中的带有java的元素的个数
  public class Test {
  public static void main(String[] args) {
      Collection<String> c=new ArrayList<>();
      c.add("asdf");
      c.add("asjavadf");
      c.add("asdfjava");
      c.add("asdf");
      c.add("assssjavadf");
      int count = 0;
      //通过集合来获取迭代器
      Iterator<String> it = c.iterator();
      //通过迭代器方法hasNext和next来取出集合中的元素
      while (it.hasNext()){
          String next = it.next();
          //判断是否带有"java",用contains可以检测出是否包含"java"
         if(next.contains("java")){
             count++;
         }
      }
      System.out.println(count);
  }
}
###1.5集合的案例-Collection集合存储学生对象并遍历【应用】

- 案例需求

创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

- 代码实现

- 学生类

```java
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;
    }
}
  • 测试类
public class CollectionDemo {
    public static void main(String[] args) {
        //创建Collection集合对象
        Collection<Student> c = new ArrayList<Student>();

        //创建学生对象
        Student s1 = new Student("林青霞", 30);
        Student s2 = new Student("张曼玉", 35);
        Student s3 = new Student("王祖贤", 33);

        //把学生添加到集合
        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());
        }
    }
}
  	
## 2. List集合
  ### 2.1 集合特点
  	1.可以存储重复元素 
  	2.有索引
  	3.有序(存储的顺序和取出的顺序一致)
  	
  ### 2.2List集合的特有方法【应用】

| 方法名                          | 描述                                   |
| ------------------------------- | -------------------------------------- |
| void add(int index,E   element) | 在此集合中的指定位置插入指定的元素     |
| E remove(int   index)           | 删除指定索引处的元素,返回被删除的元素 |
| E set(int index,E   element)    | 修改指定索引处的元素,返回被修改的元素 |
| E get(int   index)              | 返回指定索引处的元素            
  
  *注意: 凡是跟索引有关的方法, 要注意不要索引越界
  ###2.3 List集合的遍历
  1.可以采用迭代器
  2.采用普通for循环
  2.采用增强for循环(缺点,不能再对集合进行增删操作了)

- 案例需求

创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

- 代码实现

- 学生类

  ```java
  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;
      }
  }
  ```

- 测试类

  ```java
  public class ListDemo {
      public static void main(String[] args) {
          //创建List集合对象
          List<Student> list = new ArrayList<Student>();
  
          //创建学生对象
          Student s1 = new Student("林青霞", 30);
          Student s2 = new Student("张曼玉", 35);
          Student s3 = new Student("王祖贤", 33);
  
          //把学生添加到集合
          list.add(s1);
          list.add(s2);
          list.add(s3);
  
          //迭代器方式
          Iterator<Student> it = list.iterator();
          while (it.hasNext()) {
              Student s = it.next();
              System.out.println(s.getName() + "," + s.getAge());
          }
          
          System.out.println("--------");
  
          //for循环方式
          for(int i=0; i<list.size(); i++) {
              Student s = list.get(i);
              System.out.println(s.getName() + "," + s.getAge());
          }
  
      }
  }
  
  ### 2.4并发修改异常【应用】

- 出现的原因
  在迭代器遍历集合的时候采用集合的方法对集合进行增删的操作,导致迭代器在获取元素中判断预期修改的值与实际修改的值不一样,
  就会发生并发修改异常(ConcurrentModificationException)。
- 解决的方案

	1.for循环遍历,然后用集合对象做对应的操作即可
  2.用迭代器自身的revenue方法
  
-示例代码	
public class Test04 {
      public static void main(String[] args) {
          // 1. 创建集合对象
          ArrayList<String> list = new ArrayList<>();
          // 2. 添加集合元素
          list.add("a");
          list.add("b");
          list.add("c");
          list.add("d");

          // 3. 获取迭代器
          Iterator<String> it = list.iterator();
          // 4. 循环遍历内部是否还有元素
          while(it.hasNext()){
              // 5. 取出元素
              String content = it.next();
              // 6. 判断是否是要删除的元素
              if(content.equals("b")){
                  //解决并发修改异常,改用迭代器自身的revenue方法
                  it.remove();
              }
          }
          // 7. 打印最终集合元素
          System.out.println(list);
      }
  }
### 2.5增强for循环【应用】(有弊端  不能进行增删的操作)

- 定义格式
  本质: 底层使用还是迭代器,小心并发修改异常,
  在使用增强for的时候不要采用集合方法对集合进行增删操作

for(集合或者数组元素的数据类型 变量名 : 数组/集合对象名) {
    循环体;
}	
  
  **用快捷键实现:集合名.for
  

## 3.数据结构

### 3.1数据结构之栈和队列【记忆】

- 栈结构

	先进后出

- 队列结构

	先进先出

### 3.2数据结构之数组和链表【记忆】

- 数组结构(连续的空间)

	查询快、增删慢
  查询快原因:因为有索引可以按照索引来查
  增删慢:数组的长度是固定的,如果要增删元素都需要重新建一个新的数组,然后将原来数组中额元素一个一个放进去

- 队列结构(散列的空间)

	查询慢、增删快	
  增删块原因:如果要增加, 只需要断开两个节点就可以了
  查询慢原因: 单链表必须从头开始查,双链表, 可以从头或者从尾开始查
  
  ##4 List集合子类的特点	
  ArrayList和LinkedList
  	ArrayList:底层是数组
  	LinkedList:底层是链表	
### 4.2集合的案例-ArrayList集合存储学生对象三种方式遍历【应用】

- 案例需求

?	创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

- 代码实现

- 学生类

  ```java
  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;
      }
  }
  ```

- 测试类

  ```java
  public class ArrayListDemo {
      public static void main(String[] args) {
          //创建ArrayList集合对象
          ArrayList<Student> array = new ArrayList<Student>();
  
          //创建学生对象
          Student s1 = new Student("林青霞", 30);
          Student s2 = new Student("张曼玉", 35);
          Student s3 = new Student("王祖贤", 33);
  
          //把学生添加到集合
          array.add(s1);
          array.add(s2);
          array.add(s3);
  
          //迭代器:集合特有的遍历方式
          Iterator<Student> it = array.iterator();
          while (it.hasNext()) {
              Student s = it.next();
              System.out.println(s.getName() + "," + s.getAge());
          }
          System.out.println("--------");
  
          //普通for:带有索引的遍历方式
          for(int i=0; i<array.size(); i++) {
              Student s = array.get(i);
              System.out.println(s.getName() + "," + s.getAge());
          }
          System.out.println("--------");
  
          //增强for:最方便的遍历方式
          for(Student s : array) {
              System.out.println(s.getName() + "," + s.getAge());
          }
      }
  }
  ```

### 4.3LinkedList集合的特有功能【应用】

- 特有方法

| 方法名                    | 说明                             |
| ------------------------- | -------------------------------- |
| public void addFirst(E e) | 在该列表开头插入指定的元素       |
| public void addLast(E e)  | 将指定的元素追加到此列表的末尾   |
| public E getFirst()       | 返回此列表中的第一个元素         |
| public   E getLast()      | 返回此列表中的最后一个元素       |
| public E removeFirst()    | 从此列表中删除并返回第一个元素   |
| public   E removeLast()   | 从此列表中删除并返回最后一个元素 |
  
  
  
  
  
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值