day05 List集合

1.Collection集合

1.1集合体系结构【记忆】

  • 集合类的特点

    ​ 提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变

  • 集合类的体系图

    在这里插入图片描述

1.2Collection集合概述和基本使用【应用】

  • Collection集合概述

    • 是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素
    • JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现
  • 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.3Collection集合的常用方法【应用】

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

1.4Collection集合的遍历【应用】

  • 迭代器的介绍
    • 迭代器,集合的专用遍历方式
    • Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
    • 迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
  • Collection集合的遍历
public class IteratorDemo {
    public static void main(String[] args) {
        //创建集合对象
        Collection<String> c = new ArrayList<>();

        //添加元素
        c.add("hello");
        c.add("world");
        c.add("java");
        c.add("javaee");

        //Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
        Iterator<String> it = c.iterator();

        //用while循环改进元素的判断和获取
        while (it.hasNext()) {
            String s = it.next();
            System.out.println(s);
        }
    }
}

1.5集合使用步骤图解【理解】

  • 使用步骤

在这里插入图片描述

1.6集合的案例-Collection集合存储学生对象并遍历【应用】

  • 案例需求

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

  • 代码实现

    • 学生类
    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.1List集合概述和特点【记忆】

  • List集合概述
    • 有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素
    • 与Set集合不同,列表通常允许重复的元素
  • List集合特点
    • 有索引
    • 可以存储重复元素
    • 元素存取有序

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集合存储学生对象并遍历【应用】

  • 案例需求

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

  • 代码实现

    • 学生类

      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 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

  • 解决的方案

    ​ 用for循环遍历,然后用集合对象做对应的操作即可

  • 示例代码

    public class ListDemo {
        public static void main(String[] args) {
            //创建集合对象
            List<String> list = new ArrayList<String>();
    
            //添加元素
            list.add("hello");
            list.add("world");
            list.add("java");
    
            //遍历集合,得到每一个元素,看有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现
    //        Iterator<String> it = list.iterator();
    //        while (it.hasNext()) {
    //            String s = it.next();
    //            if(s.equals("world")) {
    //                list.add("javaee");
    //            }
    //        }
    
            for(int i=0; i<list.size(); i++) {
                String s = list.get(i);
                if(s.equals("world")) {
                    list.add("javaee");
                }
            }
    
            //输出集合对象
            System.out.println(list);
        }
    }
    

2.5列表迭代器【应用】

  • ListIterator介绍

    • 通过List集合的listIterator()方法得到,所以说它是List集合特有的迭代器
    • 用于允许程序员沿任一方向遍历的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置
  • 示例代码

    public class ListIteratorDemo {
        public static void main(String[] args) {
            //创建集合对象
            List<String> list = new ArrayList<String>();
    
            //添加元素
            list.add("hello");
            list.add("world");
            list.add("java");
    
            //获取列表迭代器
            ListIterator<String> lit = list.listIterator();
            while (lit.hasNext()) {
                String s = lit.next();
                if(s.equals("world")) {
                    lit.add("javaee");
                }
            }
    
            System.out.println(list);
    
        }
    }
    

2.6增强for循环【应用】

  • 定义格式

    for(元素数据类型 变量名 : 数组/集合对象名) {
        循环体;
    }
    
    
  • 示例代码

    public class ForDemo {
        public static void main(String[] args) {
            int[] arr = {1,2,3,4,5};
            for(int i : arr) {
                System.out.println(i);
            }
            
            System.out.println("--------");
    
            String[] strArray = {"hello","world","java"};
            for(String s : strArray) {
                System.out.println(s);
            }
            
            System.out.println("--------");
    
            List<String> list = new ArrayList<String>();
            list.add("hello");
            list.add("world");
            list.add("java");
    
            for(String s : list) {
                System.out.println(s);
            }
            
            System.out.println("--------");
    
            //内部原理是一个Iterator迭代器
            /*
            for(String s : list) {
                if(s.equals("world")) {
                    list.add("javaee"); //ConcurrentModificationException
                }
            }
            */
        }
    }
    
    

2.7集合的案例-List集合存储学生对象三种方式遍历【应用】

  • 案例需求

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

  • 代码实现

    • 学生类

      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 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());
              }
              System.out.println("--------");
      
              //增强for:最方便的遍历方式
              for(Student s : list) {
                  System.out.println(s.getName()+","+s.getAge());
              }
          }
      }
      
      

3.数据结构

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

  • 栈结构

    ​ 先进后出

  • 队列结构

    ​ 先进先出

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

  • 数组结构

    ​ 查询快、增删慢

  • 队列结构

    ​ 查询慢、增删快

4.List集合的实现类

4.1List集合子类的特点【记忆】

  • ArrayList集合

    ​ 底层是数组结构实现,查询快、增删慢

  • LinkedList集合

    ​ 底层是链表结构实现,查询慢、增删快

4.2集合的案例-ArrayList集合存储学生对象三种方式遍历【应用】

  • 案例需求

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

  • 代码实现

    • 学生类

      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 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()从此列表中删除并返回最后一个元素

集合
一、集合和数组的区别是什么?
1.数组的长度不可变。而集合的长度可变
2.数组里面元素的类型既可以是基本数据类型,也可以是引用数据类型 // int[] ints = new int[10], Student[] stus = new Student[10];
集合中的元素只能是引用数据类型
3.数组中只能是单列的
集合中既可以是单列的,也可以是双列的

二、集合的体系结构
	1.单列集合-Collection
		(1)List集合:可存储重复元素、有索引、存取有序
			ArrayList 
			LinkedList
		
		(2)Set集合:不可存储重复元素,没有索引、存取无序
			HashSet 
			TreeSet
	
	2.双列集合-Map 
		HashMap
		

三、Collection集合的常用方法
	1.boolean add(E e) 添加元素  //在集合的末尾添加元素
	
	2.boolean remove(Object o) 从集合中移除指定的元素  //从0索引开始删除第一个出现的数据
	
	3.void clear() 清空集合中的元素//了解   高危操作
	
	4.boolean contains(Object o) 判断集合中是否存在指定的元素  //容易忘
		缺点:就是只能知道集合中是否存在某个元素,但是元素的位置不知道
	
	5.boolean isEmpty() 判断集合是否为空  //不要用
	
	6.int size() 集合的长度,也就是集合中元素的个数
		

	//扩展
		1.判空:判断集合不为空
			空:有两层意思,第一层是不能为null;
						第二层是集合中的元素数量要大于0
			if(null!=list && list.size()>0){
				xxxxxxxxxxx
			}
		2.leng、length()、size()三者的区别?
			length:数组的长度(属性)
			length():字符串的长度(方法)
			size():集合的长度(方法)
		
		3.List集合删除元素的时候,有什么注意事项?
			//反向遍历   或者   添加i--
		
四、Collection集合的遍历
	1.为什么Collection集合不用普通for循环来遍历?
		因为Collection集合是单列集合的顶层接口,有两个子接口List和Set,而Set集合是没有索引的,所以用不了get(index)这个方法
		
	2.迭代器是通过集合的iterator()方法得到的,所以我们说它是 "依赖于集合而存在的"
	
	3.相关的方法
		Iterator<E> iterator​() 返回此集合中元素的迭代器。  
		//默认有一个类似于指针的东西,默认位置在元素的最上方
		
		boolean hasNext​() 如果迭代具有更多元素,则返回 true 。 	
			//只做一件事
			*判断有没有下一个元素
			
		E next​() 返回迭代中的下一个元素。
			//做了两件事
			*第一件事:获取下一个元素的值
			*第二件事:将指针向下移动一位
				//注意事项:next方法不要重复使用
		
	4.遍历的步骤
		*得到迭代器对象
			 Iterator<Integer> iterator = arr.iterator();
		*使用迭代器对象去判断集合中是否有元素
		*使用迭代器元素对象取出元素
			 while (iterator.hasNext()){
					int temp = iterator.next();
					System.out.println(temp);
				}
	
	5.Collection集合存储学生对象并遍历
		//难点:使用迭代器去遍历学生对象集合

五、List集合
	1.软件包  java.util 
		Interface List<E>
		//使用的util包下的List

	2.特点
		(1)存取有序
		(2)可重复
		(3)有索引

	3.List集合的"特有"方法
		(1)void add(int index,E element) 在此集合中的指定位置插入指定的元素
		(2)E remove(int index) 删除指定索引处的元素,返回被删除的元素   //掌握
		(3)E set(int index,E element) 修改指定索引处的元素,返回被修改的元素  //掌握
		(4)E get(int index) 返回指定索引处的元素   //掌握
		//注意:这四个方法都有一个共同点,就是参数里面都有索引
			*这里的remove方法和Collection中的remove方法的区别
				List集合的remove方法是根据"索引"删除
				Collection中的remove方法是根据元素删除
		
	4.List集合存储和遍历学生对象
		(1)创建集合对象的时候,要是用List接口多态
		(2)加上集合的判空
		(3)使用迭代器和普通for循环两种方式来遍历
			代码实现:
				public class Demo3 {
				public static void main(String[] args) {
					List<Student> list1 = new ArrayList<>();

					Student s1 = new Student("王小二",12);
					Student s2 = new Student("王小三",13);
					Student s3 = new Student("王小四",14);
					Student s4 = new Student("王小五",15);
					Student s5 = new Student("王小六",16);

					list1.add(s1);
					list1.add(s2);
					list1.add(s3);
					list1.add(s4);
					list1.add(s5);

					if (null!=list1 && list1.size()>0){  //集合的判空
						//使用迭代器方式遍历
						Iterator<Student> iterator = list1.iterator();
						while (iterator.hasNext()){
							System.out.println(iterator.next());
						}

						//使用普通for循环的方式遍历
						for (int i = 0; i < list1.size(); i++) {
							System.out.println(list1.get(i));
						}


					}

				}
			}
			
			ArrayList<Student> list = new ArrayList<>();

			泛型是Student,所以可以存储Student对象

			ArrayList<Object> list = new ArrayList<>();

			泛型是Object.可以存储任意类型的对象.

			ArrayList list = new ArrayList();

			不定义泛型,默认就是Object的.所以也能存储Student对象.
		
	5.并发修改异常   //了解
		//视频中的源码分析不需要大家掌握
		(1)概念
			"迭代器遍历的过程中",通过集合对象修改了集合中的元素,造成了迭代器获取元素中判断预期修改值和实际
				修改值不一致,则会出现:ConcurrentModificationException
			
		(2)并发修改异常的前提:"迭代器遍历的过程中"
				前提就是一定要在迭代器遍历的过程中
					代码体现:
						Iterator<Student> iterator = list1.iterator();   //迭代器遍历开始
						while (iterator.hasNext()){
							System.out.println(iterator.next());
						}                                                //迭代器遍历结束

		(3)并发修改异常产生的条件
			并:并行,同时
			发:发生
			并发:同时发生   //必须至少要有两个对象
			修改:指的是"添加"或者"删除"元素
			
			//并发修改异常产生的条件
				*第一个条件:至少有两个对象
				*第二个条件:要添加或者删除元素

	6.列表迭代器
		(1)列表迭代器的获取
			ListIterator<E> listIterator​() 返回列表中的列表迭代器(按适当的顺序)。  

		(2)列表迭代器特有方法   
			*void add​(E e) 将指定的元素插入列表(可选操作)。  
			
			*boolean hasPrevious​() 如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回 true 。  
				//相比较hasNext方法学习    只做一件事:判断有没有上一个元素
			*E previous​() 返回列表中的上一个元素,并向后移动光标位置。  
				//相比较next方法学习   做了两件事:
					*第一件事:获取上一个元素的值
					*第二件事:将指针向上移动一位
						//注意事项:previous​方法不要重复使用
				代码实现:
					public class Demo5 {
					public static void main(String[] args) {
						List<String> list1 = new ArrayList<>();
						list1.add("王小二");
						list1.add("王小三");
						list1.add("王小四");
						list1.add("王小五");
						list1.add("王小六");

						//获取列表迭代器对象
						ListIterator<String> ite = list1.listIterator();
						while (ite.hasNext()){
							System.out.println(ite.next());
						}

						System.out.println("--------------------");

						while (ite.hasPrevious()){
							System.out.println(ite.previous());
						}

					}
				}
				
			//注意:hasPrevious​和previous​不常用,因为使用这两个方法的前提是需要先正向遍历到末尾。

		(3)研究列表迭代器的add方法
			*第一点:列表迭代器的add方法和list集合的add方法的区别?
				列表迭代器的add方法:在当前元素的后面添加
				list集合的add方法:在集合的末尾添加

			*为什么没有产生并发修改异常
				思路:想要产生并发修改异常需要有一个前提和两个条件
					前提:在迭代器遍历的过程中  --------  符合
					条件一:至少有两个操作集合的对象   -------------  //不符合
					
					条件二:要添加或者删除元素    -------------  符合
	
	7.增强for循环
		(1)格式
			for(元素数据类型 变量名 : 数组/集合对象名) {
				循环体;
				}
				
				代码举例:
					for (String s : list1){
						   System.out.println(s);
					   }
		
		(2)本质
			增强for循环其实就是用来简化迭代器的代码的,底层就是迭代器

		(3)好处
			简化了迭代器的步骤

		(4)注意事项
			在使用增强for循环之前,一定要对集合判空
				代码举例:
					List<String> list = null;

					if (null!=list && list.size()>0){
						for (String s : list){
							System.out.println(s);
						}
					}

	8.list集合三种遍历方式的总结
		(1)迭代器     
		(2)普通for循环   //涉及到增删元素
		(3)增强for循环   //仅仅遍历

数据结构相关知识 //重点掌握它们的特点
1.栈 : 进栈 /压栈 出栈/弹栈
特点:先进后出

2.队 :  入队列   出队列
	特点:先进先出

3.数组:存储同一种类型的多个元素的容器  //有索引
	特点:查询快、增删慢
	应用:ArrayList

4.链表:由一个链子把多个节点连接起来组成的数据
	节点:由数据和地址组成
	特点:查询慢、增删快
	应用:LinkedList


5.List集合子类的特点   //笔试题     掌握
	ArrayList: 
		底层结构是数组,查询快、增删慢
		线程不安全的、效率高
	
	Vector:
		底层结构是数组,查询快、增删慢 
		线程安全的,效率低
	
	LinkedList:
		底层结构是链表,查询慢、增删快
		线程不安全的,效率高

6.LinkedList集合的特有功能    //了解
	public void addFirst(E e) 在该列表开头插入指定的元素
	public void addLast(E e) 将指定的元素追加到此列表的末尾
	public E getFirst() 返回此列表中的第一个元素
	public E getLast() 返回此列表中的最后一个元素
	public E removeFirst() 从此列表中删除并返回第一个元素
	public E removeLast() 从此列表中删除并返回最后一个元素

List集合的总结
//以后基本上使用ArrayList
增:
*boolean add(E e) 添加元素 //在集合的末尾添加元素

	删:
		*boolean remove(Object o) 从集合中移除指定的元素  //从0索引开始删除第一个出现的数据
		*E remove(int index) 删除指定索引处的元素,返回被删除的元素   //掌握
	
	改:
		*E set(int index,E element) 修改指定索引处的元素,返回被修改的元素  //掌握
	
	遍历:
		*迭代器遍历 
		*普通for循环遍历
		*增强for循环遍历
	
	判空:
		if(null!=list && list.size()>0){
				xxxxxxxxxxx
			}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值