美文网首页
7月24号_list集合

7月24号_list集合

作者: 须臾之北 | 来源:发表于2018-07-25 21:54 被阅读26次

List的学习

今天的主要内容

  • 集合框架(去除ArrayList中重复字符串元素方式)(掌握)

  • 集合框架(去除ArrayList中重复自定义对象元素)(掌握)

  • 集合框架(LinkedList的特有功能)(掌握)

  • 集合框架(用LinkedList模拟栈数据结构的集合并测试)(掌握)

  • 集合框架(三种迭代的能否删除)(掌握)

  • 集合框架(三种迭代的能否删除)(掌握)

一、集合框架(去除ArrayList中重复字符串元素方式)(掌握)

  1. 案例演示

    • 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)

    • 思路:创建新集合方式

      • 分析:
        1. 创建新集合
        2. 获取老集合中的迭代器
        3. 遍历老集合
        4. 通过新集合判断是否包含老集合中的元素,如果包含则不添加,不包含则添加
    • 此时要注意的是:从老集合中获取,到新的中查看是否存在

2 .程序

        /*
         * * A:案例演示
            * 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
            * 思路:创建新集合方式
            * 
            * 分析:
            *   1. 创建新集合
            *   2. 获取老集合中的迭代器
            *   3.遍历老集合
            *   4. 通过新集合判断是否包含老集合中的元素,如果包含则不添加,不包含则添加
         * */
        public class TestList_1 {
            public static void main(String[] args) {
                ArrayList<String> list = new ArrayList<>();
                
                list.add("haha");
                list.add("xixi");
                list.add("haxi");
                list.add("xiha");
                list.add("haha");
                list.add("xixi");
                list.add("haxi");
                list.add("xiha");
                
                System.out.println("======原集合中元素为======");
                System.out.println(list);
                
                System.out.println("======去重后集合中元素为======");
                System.out.println(getList(list));      
            }
        
            private static ArrayList<String> getList(ArrayList<String> list) {
                //创建新集合
                ArrayList<String> newList = new ArrayList<>();
                
                //获取老集合的迭代器     
                Iterator<String> i = list.iterator();
                
                while(i.hasNext()) {
                    //获取老集合中元素
                    String s = i.next();
                    
                    //如果新集合中不包含则add
                    if(!newList.contains(s))
                        newList.add(s);
                }       
                return newList;
            }
        }
        /*
         在JDK1.8中输出结果为:
        ------------------------------------------------
        ======原集合中元素为======
        [haha, xixi, haxi, xiha, haha, xixi, haxi, xiha]
        ======去重后集合中元素为======
        [haha, xixi, haxi, xiha]
        ------------------------------------------------
        */

二、集合框架(去除ArrayList中重复自定义对象元素)(掌握)

  • 自定义Employee类并存储对象到集合中,然后去重

    • 重写equals()方法的
  • class Employee

      class Employee{
    
          //data field
          private String name;
          private double salary;
          private LocalDate hireDay;
          
          //constructor
          public Employee(String name,double salary,int year,int month,int day){
              this.name = name;
              this.salary = salary;
              this.hireDay = LocalDate.of(year,month,day);
          }
          
          //method
          public String getName(){
              return name;    
          }
          
          public double getSalary(){
              return salary;  
          }
          
          public LocalDate gethireDay(){
              return hireDay;
          }
          
          public void raiseSalary(double byPercent){
              salary += salary * byPercent/100;
          }
          
          public String toString() {
              return "name : " + name + " salary : " + salary + " hireDay : " + hireDay;      
          }
      
          @Override
          public boolean equals(Object obj) {
              if(this == obj)
                  return true;
              
              if(obj == null)
                  return false;
              
              if(this.getClass() != obj.getClass())
                  return false;
              
              Employee e = (Employee)obj;     
              return (this.name.equals(e.name) && this.salary == e.salary && this.hireDay.equals(e.hireDay));
          }
      }           
    
  • class TestList_2

      public class TestList_2 {
          public static void main(String[] args) {
              ArrayList<Employee> list = new ArrayList<>();
              
              list.add(new Employee("haha",200,2018,7,16));
              list.add(new Employee("xixi",200,2018,7,16));
              list.add(new Employee("haxi",200,2018,7,16));
              list.add(new Employee("xiha",200,2018,7,16));
              
              list.add(new Employee("haha",200,2018,7,16));
              list.add(new Employee("xixi",200,2018,7,16));
              list.add(new Employee("haxi",200,2018,7,16));
              list.add(new Employee("xiha",200,2018,7,16));
              
              System.out.println("======原集合中元素为======");
              for(int i = 0;i < list.size();i++) {
                  System.out.println(list.get(i));
              }
              
              System.out.println("======去重后集合中元素为======");
              ArrayList<Employee> newList = getList(list);
              for(int i = 0;i < newList.size();i++) {
                  System.out.println(newList.get(i));
              }
          }
          
          private static ArrayList<Employee> getList(ArrayList<Employee> list) {
              //创建新集合
              ArrayList<Employee> newList = new ArrayList<>();
              
              //获取老集合的迭代器     
              Iterator<Employee> i = list.iterator();
              
              while(i.hasNext()) {
                  //获取老集合中元素
                  Employee s = i.next();
                  
                  //如果新集合中不包含则add
                  if(!newList.contains(s))
                      newList.add(s);
              }       
              return newList;
          }
      }
      /*
       *  在JDK1.8中输出结果为:
       *  --------------------------------------------------------
       *  ======原集合中元素为======
          name : haha salary : 200.0 hireDay : 2018-07-16
          name : xixi salary : 200.0 hireDay : 2018-07-16
          name : haxi salary : 200.0 hireDay : 2018-07-16
          name : xiha salary : 200.0 hireDay : 2018-07-16
          name : haha salary : 200.0 hireDay : 2018-07-16
          name : xixi salary : 200.0 hireDay : 2018-07-16
          name : haxi salary : 200.0 hireDay : 2018-07-16
          name : xiha salary : 200.0 hireDay : 2018-07-16
          ======去重后集合中元素为======
          name : haha salary : 200.0 hireDay : 2018-07-16
          name : xixi salary : 200.0 hireDay : 2018-07-16
          name : haxi salary : 200.0 hireDay : 2018-07-16
          name : xiha salary : 200.0 hireDay : 2018-07-16
          --------------------------------------------------------
       * */
    

三、集合框架(LinkedList的特有功能)(掌握)

  1. LinkedList类概述

    • LinkedList类特有功能
    • public void addFirst(E e)及addLast(E e)
    • public E getFirst()及getLast()
    • public E removeFirst()及public E removeLast()
    • public E get(int index);
  2. 程序

     public class TestLinkedList_1 {
         public static void main(String[] args) {
             LinkedList<String> list = new LinkedList<>();
             
             list.addFirst("1");       //头部插入,插入顺序和输出顺序相反
             list.addFirst("2");
             list.addFirst("3");
             list.addLast("i");     //尾部插入,则输入顺序和输出顺序一致
             list.addLast("love");
             list.addLast("you");;
             
             System.out.println("======list集合中的元素======");
             list.forEach((str) ->{
                 System.out.print (str + "  ");
             });
             
             System.out.println();
             System.out.println();
             
             //获取0号位置上元素
             System.out.println("====get(int index)获取元素====");
             System.out.println("第 0 个位置上元素为:" + list.get(0));
             
             System.out.println();
             
             //删除头部和尾部
             System.out.println("======删除头部和尾部后======");
             list.forEach((str) ->{
                 System.out.print(str + "  ");
             });
         }
     }
     /*
      *  在JDK1.8中输出结果为:
      *  -----------------------------
      *  ======list集合中的元素======
         3  2  1  i  love  you  
     
         ====get(int index)获取元素====
         第 0 个位置上元素为:3
         
         ======删除头部和尾部后======
         -----------------------------
         2  1  i  love  
      * */
    

四、集合框架(用LinkedList模拟栈数据结构的集合并测试)(掌握)

  • 创建Stack类,利用LinkedList来实现进栈、出栈和判空

      class Stack<E> {
          private LinkedList<E> list = new LinkedList<>();
          
          //元素添加
          void add(E e) {
              list.addFirst(e);
          }
          
          //元素取出
          E get() {
              return list.removeFirst();
          }
          
          boolean isEmpty() {
              return list.isEmpty();
          }
      }
    
  • Stack测试类——TestStack

      public class TestStack {
          public static void main(String[] args) {
              Stack<String> stack  = new Stack<>();
              
              stack.add("1");
              stack.add("2");
              stack.add("3");
              stack.add("4");
              
              while(!stack.isEmpty()) {
                  System.out.print(stack.get() + "   ");
              }       
          }
      }
      /*
       * 在JDK1.8中输出结果为:
       * ---------------
       * 4   3   2   1 
       * ---------------
       * */
    

五、集合框架(泛型概述和基本使用)(掌握)

  1. 泛型概述

  2. 泛型好处

    • 提高安全性(将运行期的错误转换到编译期)

    • 省去强转的麻烦

  3. 泛型基本使用

    • <>中放的必须是引用数据类型
  4. 泛型使用注意事项

    • 前后的泛型必须一致,或者后面的泛型可以省略不写

六、集合框架(三种迭代的能否删除)(掌握)

  • 普通for循环,可以删除,但是索引要--

      List<String> list = new ArrayList<>();
      
      list.add("a");
      list.add("b");
      list.add("b");
      list.add("d");
      
      System.out.println("======list中元素======");
      System.out.println(list);
      
      System.out.println("=====删除元素'b'后=====");
      //for循坏进行元素删除
      for(int i = 0;i < list.size();i++) {
          if(list.get(i).equals("b")) {
              //list.remove(i);        
              //remove以后数组元素上移,i本来指向了下一个待检查的元素,但是i++使得指针下移,造成元素遗漏
              list.remove(i--);
          }       
      }
      System.out.println(list);
    
      /*
       * 在JDK1.8中输出结果为:
       * --------------------------
       * ======list中元素======
         [a, b, b, d]
         =====删除元素'b'后=====
         [a, b, d]                   //无法做到正确删除b,i的问题
         -------------------------
       * */
    
  • 迭代器,可以删除,但是必须使用迭代器自身的remove方法,否则会出现并发修改异常

      List<String> list = new ArrayList<>();
      
      list.add("a");
      list.add("b");
      list.add("b");
      list.add("d");
      
      System.out.println("======list中元素======");
      System.out.println(list);
      
      System.out.println("=====删除元素'b'后=====");
              
      for(Iterator<String> iterator = list.iterator();iterator.hasNext();) {
          if("b".equals(iterator.next()))
              iterator.remove();          //不可调用集合的remove方法
      }
      
      System.out.println(list);
    
      /*
       *   在JDK1.8中输出结果为:
       *  ------------------------
       *  ======list中元素======
          [a, b, b, d]
          =====删除元素'b'后=====
          [a, d]                      //成功删除
         -------------------------
       * */
    
  • 增强for循环不能删除

    • 有异常,增强的for循坏不可以删除元素,只能遍历

    • 因为增强的for循坏的原理就是迭代器,迭代器中不可以通过集合名称来删除元素

七、集合框架(集合嵌套之ArrayList嵌套ArrayList)(掌握)

  • A:案例演示

    • 集合嵌套之ArrayList嵌套ArrayList
  • 程序

      public class TestMulArrayList {
          public static void main(String[] args) {
              ArrayList<Employee> personList1 = new ArrayList<>();   //第一个部门员工信息
              ArrayList<Employee> personList2 = new ArrayList<>();    //第二个部门员工信息
              
              //为第一个部门员工添加信息
              personList1.add(new Employee("haha", 200, 2018, 7, 24));
              personList1.add(new Employee("xixi", 300, 2018, 7, 24));        
              
              //为第二个部门员工添加信息
              personList2.add(new Employee("haxi", 400, 2018, 7, 24));
              personList2.add(new Employee("xiha", 500, 2018, 7, 24));
              
              ArrayList<ArrayList<Employee>> depList = new ArrayList<>();
              
              depList.add(personList1);
              depList.add(personList2);   
              
              for(ArrayList<Employee> elem : depList) {
                  System.out.println(elem);
              }  
          }
      /* 在JDK1.8中输出结果为:
       * [name : haha salary : 200.0hireDay : 2018-07-24, name : xixi salary : 300.0hireDay : 2018-07-24]
         [name : haxi salary : 400.0hireDay : 2018-07-24, name : xiha salary : 500.0hireDay : 2018-07-24]
       * */
    

相关文章

  • java基础-day16-list集合

    List集合 1. List集合 1.1 List集合特征 1.2 List常用方法 2. ArrayList 可...

  • kotlin学习第五天:集合,高阶函数,Lambda表达式

    集合 list集合 list集合分为可变集合与不可变集合。由list of创建的集合为不可变集合,不能扩容,不能修...

  • 2019-01-01

    基于List、Set集合,及工具类Collections集合元素排序整理 List集合 1.1 List集合介绍 ...

  • kotlin练习 ---- 集合练习

    kotlin练习 - 集合练习 Set集合 Set集合创建 Set集合的使用 List集合 List集合创建 Li...

  • java集合之List接口

    List集合存储元素的特点 1、有序(List集合中存储下标) 2、可重复 深入List集合 ArrayList集...

  • 数组和集合(二)

    List集合 与 Java类似,List集合的最大特征就是集合元素都有对应的顺序索引 List集合允许使用重复元素...

  • JAVA基础——List集合

    List集合 List集合代表一个元素有序、可重复的集合、集合中每个元素都有其对应的顺序索引。List集合允许使用...

  • kotlin精讲-第5章(9)list-上

    list集合介绍 list集合怎么定义呢?从概念角度去理解,list集合也叫list列表,存储的元素有序可重复。有...

  • 一文彻底了解List集合

    一、List集合 List集合是Collection子类。ArrayList、LinkedList、Vector分...

  • Java Collection集合

    Collection集合 collection集合作为List集合和Set集合共有的父类拥有着List集合和Set...

网友评论

      本文标题:7月24号_list集合

      本文链接:https://www.haomeiwen.com/subject/rcwmmftx.html