JavaSE02_Day02(上中)-remove、迭代器、新循环、泛型、List(ArrayList、LinkList):set、get、add、remove、subList、数组与集合互相转化

一、Collection集合接口的相关方法

1.1 remove:从集合中删除指定元素内容

 package cn.tedu.collection;
 
 import java.util.ArrayList;
 import java.util.Collection;
 
 /**
  * 删除集合元素案例
  * @author cjn
  *
  */
 public class Collection_remove {
 
     public static void main(String[] args) {
         //1.创建集合对象,指定集合中添加元素类型为Point类型
         Collection<Point> c = new ArrayList<Point>();
         
         //2.向集合中添加点元素
         c.add(new Point(1, 2));
         c.add(new Point(3, 4));
         c.add(new Point(5, 6));
         c.add(new Point(7, 8));
         c.add(new Point(9, 0));
         c.add(new Point(1, 2));
         //输出集合内容
         System.out.println("当前集合对象:" + c);
         
         /*
          * 准备需要删除点的数据内容
          * 注意:
          * 如果集合中含有多个内容一样的元素,
          * 当调用remove方法进行删除集合中相同元素时,
          * 会按顺序进行比较查找,删除第一个equals比较结果
          * 为true的集合中元素,删除后,立即停止后面的删除操作。
          * 即:remove一次只能删除一个集合元素,并不能删除全部重复元素!
          */
         Point point = new Point(1, 2);
         c.remove(point);//删除点为(1,2)的集合元素
         System.out.println("删除元素以后,集合对象:" + c);
         System.out.println("删除元素以后,集合中元素个数为:" + c.size());
 
    }
 
 }

输出结果:

 当前集合对象:[Point [x=1, y=2], Point [x=3, y=4], Point [x=5, y=6], Point [x=7, y=8], Point [x=9, y=0], Point [x=1, y=2]]
 删除元素以后,集合对象:[Point [x=3, y=4], Point [x=5, y=6], Point [x=7, y=8], Point [x=9, y=0], Point [x=1, y=2]]
 删除元素以后,集合中元素个数为:5

 

1.2 集合和集合进行相关操作的API方法

  • boolean addAll(Collection c):判断是否成功添加所有集合元素(将某一集合中的元素添加到另一集合中,可以是重复的元素,添加到原集合元素的末尾)

  • boolean containsAll(Collection c):判断是否包含集合中全部元素(判断某一集合是否包含另一集合中的全部元素)

  • boolean removeAll(Collection c):判断是否成功移除集合中全部元素(移除/删除某一集合中与该集合元素全部相同的元素内容,可以存在不同的元素,只删除相同的元素内容)

 package cn.tedu.collection;
 /**
  * 集合对集合进行操作的案例
  * @author cjn
  *
  */
 
 import java.util.ArrayList;
 import java.util.Collection;
 
 public class Conllection_Conllection {
     
     public static void main(String[] args) {
         //1.创建集合对象
         Collection<String> c1 = new ArrayList<String>();
         
         //2.向集合中添加元素内容
         c1.add("WEB");
         c1.add("Swift");
         c1.add("PHP");
         c1.add("Java");
         System.out.println("集合c1对象:" +c1);
         
         //3.创建集合对象
         Collection<String> c2 = new ArrayList<String>();
         
         //4.向集合中添加元素内容
         c2.add("c");
         c2.add("python");
         c2.add("big");
         c2.add("Java");
         c2.add("oc");
         System.out.println("集合c2对象:" +c2);
         
         /*
          * 5.boolean addAll(Collection c)
          * 该方法可以将给定集合c1中的所有元素内容,添加到集合c2中。
          * 返回值为true表示集合元素添加成功。
          * 注意:
          * 目前创建集合使用的实现类是List接口下的实现类,
          *     所以当前一个集合向另一个集合添加元素时允许有重复的情况。
          */
         boolean b = c2.addAll(c1);
         System.out.println("是否添加成功:" + b);
         System.out.println("c2集合对象:" + c2);
         System.out.println("c1集合对象:" + c1);
         
         /*
          * 6.boolean containsAll(Collection c)
          * 该方法可以进行判断当前集合是否包含给定的集合中所有元素内容
          *
          */
         Collection<String> c3 = new ArrayList<String>();
         c3.add("Java");
         c3.add("python");
         c3.add(".net");
         b = c2.containsAll(c3);
         System.out.println("c2集合中是否包含c3集合中所有元素:" + b);
         
         /*
          * 7. boolean removeAll(Collection c)
          * 该方法可以删除当前集合与给定集合中共有的元素内容
          */
         c2.removeAll(c3);
         System.out.println("c2集合中最终的元素内容:" + c2);
         System.out.println("c3集合中最终的元素内容:" + c3);    
    }
 
 }

测试结果:

 集合c1对象:[WEB, Swift, PHP, Java]
 集合c2对象:[c, python, big, Java, oc]
 是否添加成功:true
 c2集合对象:[c, python, big, Java, oc, WEB, Swift, PHP, Java]
 c1集合对象:[WEB, Swift, PHP, Java]
 c2集合中是否包含c3集合中所有元素:false
 c2集合中最终的元素内容:[c, big, oc, WEB, Swift, PHP]
 c3集合中最终的元素内容:[Java, python, .net]

 注意:上述集合操作只影响被操作集合元素的内容,对作为参数传入的集合并不影响其元素内容。

 

1.3 迭代器

       Iterator是一个接口,称之为迭代器,规定了迭代器的实现类所有实现遍历集合的通用方法。

  • boolean hasNext():判断集合中是否还有元素可以进行遍历

  • E next():返回迭代的的下一个元素

    遍历集合遵循的原则"先问后取",也就是先确认好调用hasNext方法以后的返回值为true,即有元素可以进行遍历的时候,再通过next方法取出元素。

 package cn.tedu.collection;
 
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 
 /**
  * 使用迭代器遍历集合中元素案例
  * @author cjn
  *
  */
 public class Collection_iterator {
 
     public static void main(String[] args) {
         //1.创建集合对象
         Collection<String> c1 = new ArrayList<String>();
         
         //2.向集合中添加元素内容
         c1.add("one");
         c1.add("#");
         c1.add("two");
         c1.add("#");
         c1.add("three");
         c1.add("#");
         c1.add("four");
         c1.add("one");
         System.out.println("当前集合对象c1:" + c1);
         
         //3.获取迭代器对象
         Iterator<String> iterator = c1.iterator();
         
         //4.通过迭代器进行询问是否集合中有下一个元素
         while (iterator.hasNext()) {
             //5.取出集合中的元素内容
             String str = (String)iterator.next();
             System.out.println(str);
             //删除集合中"#"
             if ("#".equals(str)) {
                 /*
                  * (1)演示错误的方案
                  * 如果使用集合对象调用删除元素方法,在控制台中会报如下错误:
                  * ConcurrentModificationException
                  * 错误的原因:因为使用迭代器进行遍历集合中元素内容时,已经获取
                  * 到了集合的名单,知道了要遍历的次数,如果使用集合的对象
                  * 进行调用remove方法,就只会删除集合中的元素,而这样迭代器
                  * 之前获取名单还是之前的样子,所以会报这个异常
                  *(即迭代器与集合中的元素名单不一样,无法进行对应操作)。
                  */
                 //c1.remove(str);
                 
                 /*
                  * (2)正确的删除方案
                  * 采用迭代器所提供的删除方法remove
                  */
                 iterator.remove();
                 
            }
        }
         
         System.out.println("集合对象:" + c1);
         
    }
 
 }

测试结果:

 当前集合对象c1:[one, #, two, #, three, #, four, one]
 one
 #
 two
 #
 three
 #
 four
 one
 集合对象:[one, two, three, four, one]

 

1.4 新循环 foreach

      新循环,也可以称之为增强for循环,也可以称之为foreach循环,是在jdk5以后所提供的特性,主要用于遍历集合或者数组对象,但是在代码的编译期间仍然会将for循环的语法转变成对应的语法结构内容。

 for(元素类型 引用对象名 :集合或者数组对象){
     //循环体
 }

(1)数组使用新循环进行遍历:

       在代码的编译期间,会将增强for循环的代码转换为普通for循环的代码结构,因为JVM虚拟机不识别新特性,所以编译器会将增强for的代码逻辑转换为普通for循环的代码逻辑。

 package cn.tedu.collection;
 /**
  * 使用新循环遍历数组
  * @author cjn
  *
  */
 public class Collection_for {
     public static void main(String[] args) {
         //1.准备数组
         String[] array = {"one","two","three","four"};
         
         //2.使用for循环遍历数组
         for (int i = 0; i < array.length; i++) {
             String str = array[i];
             System.out.println(str);
        }
         
         System.out.println("-------------------------------------");
         
         //3.使用新循环遍历数组
         for (String string : array) {
             System.out.println(string);
        }
         
    }
 }

测试结果:

 one
 two
 three
 four
 -------------------------------------
 one
 two
 three
 four

(2)集合使用新循环进行遍历:

       JVM虚拟机不支持增强for循环的语法,在代码进行编译期间会转换为迭代器的方案进行遍历。

 package cn.tedu.collection;
 
 import java.util.ArrayList;
 import java.util.Collection;
 
 /**
  * 使用新循环遍历集合
  * @author cjn
  *
  */
 public class Collection_for_two {
 
     public static void main(String[] args) {
         Collection<String> c1 = new ArrayList<String>();
         c1.add("one");
         c1.add("two");
         c1.add("three");
         c1.add("four");
         System.out.println("当前c1集合对象:" + c1);
         
         //使用新循环的方式遍历集合对象
         for (String str : c1) {
             System.out.println(str);
        }
 
    }
 
 }

测试结果:

 当前c1集合对象:[one, two, three, four]
 one
 two
 three
 four

 

二、泛型

3.1 定义

       泛型是在jdk1.5之后提出的新特性,泛型的本质是参数化类型(集合中添加元素的数据类型可以使用泛型),对参数进行数据类型限制。

3.2 作用

       泛型提供了代码在编译期间的一个安全检测机制,也就是在代码书写完报错以后,可以通过编译检测出当前往集合中添加元素的类型,是否和规定的参数化类型是一致的类型。

3.3 泛型中的通配符

  • E(Element):代表元素的含义

  • ? :表示不确定Java类型

  • T (Type):表示具体的一个Java类型

  • K V(key value):分别代表Java中的键值对

 

三、List集合

3.1 定义

       List是一个接口,继承了Collection接口,所以Collection接口所提供的API方法,List接口都可以进行使用。List集合中元素是允许有重复的,并且是有序的,当添加新元素的时候,会按照元素的添加顺序进行显示,有点类似于第一阶段所学习的数组,可以通过索引值进行访问集合中存储的元素内容。List集合也提供了实现类,ArrayList和LinkedList。

3.2 ArrayList和LinkedList区别

  • ArrayList:适合查询,可以通过索引值进行访问;不适合新增和删除元素,因为插入或删除元素的同时,其后面的所有元素都要移动。

  • LinkedList:基于链表,不适合查询,适合新增和删除元素,尤其对首尾的数据操作更加灵活。

    • 查询元素数据时不能基于索引值进行查询,只能从前往后一个元素一个元素进行查询,每个元素本身都具有一个地址值,通过其前后的地址值进行元素的连接。

    • 插入和删除元素只需要修改其前后的地址值即可。

3.3 List集合提供的相关API方法

(1)set和get

       List集合除了继承Conllection集合中所提供的API方法,也进行了功能的扩展,提供了可以根据下标进行操作的get和set方法。

  • get(int index):根据下标取出集合中指定位置的元素,下标值从0开始。

  • set(int index,E element):根据给定的下标值,替换原位置的元素,方法的返回值为原位置的元素,,下标位置从0开始。

 package cn.tedu.collection;
 
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  * List集合中set和get方法案例
  * @author cjn
  *
  */
 public class ListDemo01 {
 
     public static void main(String[] args) {
         //1.创建List集合对象
         List<String> list = new ArrayList<String>();
         
         //2.向list集合中添加元素内容
         list.add("1");
         list.add("2");
         list.add("3");
         list.add("4");
         System.out.println("集合对象为:" + list);
         
         /*
          * E get(int index)
          * 通过索引下标值进行访问指定位置的集合中元素
          */
         String str = list.get(1);
         System.out.println("下标为1的集合中元素值为:" + str);
         
         /*
          * E set(int index,E element)
          * 将指定的元素设置到给定的下标位置,返回值是被替换掉的元素
          * 业务:将索引下标为1的元素内容替换为two,生成结果为:[1,two,3,4]
          */
         String str2 = list.set(1, "two");
         System.out.println("被替换掉的集合中的元素为:" + str2);
         System.out.println("最终的集合对象:" + list);
         
 
    }
 
 }

测试结果:

 集合对象为:[1, 2, 3, 4]
 下标为1的集合中元素值为:2
 被替换掉的集合中的元素为:2
 最终的集合对象:[1, two, 3, 4]

(2)重载Collection接口中的add和remove方法

 package cn.tedu.collection;
 
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  * 重载Collection接口中add和remove方法
  * @author cjn
  *
  */
 public class ListDemo02 {
 
     public static void main(String[] args) {
         List<String> list = new ArrayList<String>();
         list.add("one");
         list.add("two");
         list.add("three");
         list.add("four");
         System.out.println("当前集合对象为:" + list);
         
         /*
          * 重载方法:void add(int index,E element)
          * 根据指定的索引位置进行添加元素内容,
          * 当在指定位置添加了元素以后,原有的元素
          * 一直到后面的元素内容都整体的向后移动
          * 业务:在索引下标位置为1的位置插入元素2,生成结果为:[one, 2, two, three, four]
          */
         list.add(1, "2");
         System.out.println("新增元素后的集合对象为:" + list);
         
         /*
          * 重载方法:E remove(int index)
          * 该方法可以删除指定下标所在集合中的元素内容,
          * 方法的返回值为被删除的元素内容
          * 业务:删除索引下标位置为1的元素,生成结果为:[one, two, three, four]
          */
         String str = list.remove(1);
         System.out.println("被删除的元素为:" + str);
         System.out.println("最终的元素对象:" + list);
         
 
    }
 
 }

测试结果:

 当前集合对象为:[one, two, three, four]
 新增元素后的集合对象为:[one, 2, two, three, four]
 被删除的元素为:2
 最终的元素对象:[one, two, three, four]

(3)截取子串subList:List<E> subList(int fromIndex, int toIndex)

       当前List集合中提供了可以进行截取子元素的API方法,该方法和String字符串的截取规则一致,遵循留前不留后(含头不含尾)的原则,需要注意的是,subList方法调用以后,获取的List集合和原有的List集合会占有相同的存储空间,如果对子集合进行修改,会影响原集合中的内容。

 package cn.tedu.collection;
 
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  * subList截取子集合的案例
  * @author cjn
  *
  */
 public class List_subList {
 
     public static void main(String[] args) {
         //创建集合对象
         List<Integer> list = new ArrayList<Integer>();
         
         //使用for循环向集合中添加元素
         for (int i = 0; i < 10; i++) {
             list.add(i);
        }
         System.out.println("集合对象为:" + list);
         
         //subList方法:List<E> subList(int fromIndex, int toIndex):指定首尾位置,含头不含尾
//业务:对集合进行截取保留[3,4,5,6,7]
         List<Integer> subList = list.subList(3, 8);
         System.out.println("截取的集合子集为:" + subList);
         
         //将子集合中的元素扩大10倍[30,40,50,60,70]:先通过get获取下标位置索引元素内容,再通过set根据索引下标按要求进行修改
         for (int i = 0; i < subList.size(); i++) {
             subList.set(i, subList.get(i)*10);
        }
         System.out.println("子集合中元素放大10倍以后内容:" + subList);
         System.out.println("原集合对象为:" + list);
 
         /*
          * 删除集合中元素索引值1~8的内容
          * 思路:对集合进行截取子集,找到1~8的子集,然后清除掉---对子集的操作就是对原始集合的操作
          */
         list.subList(1, 9).clear();
         System.out.println("原集合对象:" +list);
         
    }
 
 }

测试结果:

 集合对象为:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 截取的集合子集为:[3, 4, 5, 6, 7]
 子集合中元素放大10倍以后内容:[30, 40, 50, 60, 70]
 原集合对象为:[0, 1, 2, 30, 40, 50, 60, 70, 8, 9]
 原集合对象:[0, 9]

示意图

 

 

3.4 集合和数组之间的相互转换

(1)集合转换为数组:toArray

       如果希望将一个集合对象转换为一个数组对象,可以使用集合中提供的toArray方法进行转换,该方法并不是List集合特有的方法,而是Collection集合所提供的,也就意味着所有的集合都具备该方法的使用。

       方式1:Object[ ] toArray( );   //不常用
       方式2:<T> T[ ] toArray(T[ ] a); //常用

代码测试

 package cn.tedu.collection;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
 /**
  * 集合--->数组
  * @author cjn
  *
  */
 public class CollectionToArray {
 
     public static void main(String[] args) {
         //1.创建集合对象
         List<String> list = new ArrayList<String>();
         
         //2.向集合中添加元素内容
         list.add("one");
         list.add("two");
         list.add("three");
         list.add("four");
         list.add("five");
         list.add("six");
         list.add("seven");
         System.out.println("集合对象为:" + list);
         
         //3.调用toArray方法,将集合对象转换为数组对象
         //3.1 Object[] toArray(); 不常用
         //Object[] array = list.toArray();
         //System.out.println(Arrays.toString(array));
         
         //3.2 <T> T[] toArray(T[] a); 常用
         String[] strs = new String[list.size()];        
         String[] array = list.toArray(strs);
         System.out.println("数组对象为:"+Arrays.toString(array));
 
    }
 
 }

输出内容

 集合对象为:[one, two, three, four, five, six, seven]
 数组对象为:[one, two, three, four, five, six, seven]

注意事项

        1、如果集合转换为数组对象时,调用<T> T[] toArray(T[] a);方法,需要在方法参数中传递一个数组对象,这个数组对象如果在创建的时候,数组的长度小于集合中元素的个数,也不用担心,因为会根据实际的集合中元素个数来决定转换后的数组长度。

        2、如果集合转换为数组对象时,调用<T> T[] toArray(T[] a);方法,需要在方法参数中传递一个数组对象,这个数组对象如果在创建的时候,数组的长度大于集合中元素的个数,在进行转换的时候,多出的元素内容默认为null。

       3、一般情况下是根据实际集合中元素的个数决定转换的数组的长度。

(2)数组转换为集合:asList

       该方法是一个静态方法,这个方法是由数组的工具类Arrays所提供的,可以将一个数组对象转换为集合对象。

 static <T> List<T> asList(T... a) //...表示多个参数

      注意事项:通过数组转换为的集合对象,不能对这个集合对象进行增删元素,否则会报异常。如果说是对集合中的元素进行修改的话,会影响原数组中的元素内容(一条绳上的蚂蚱)。

 package cn.tedu.collection;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
 /**
  * 数组--->集合案例演示
  * @author cjn
  *
  */
 public class ArrayToCollection {
 
     public static void main(String[] args) {
         String[] array = {"one","two","three","four"};
         System.out.println("数组对象为:" + Arrays.toString(array));
         
         //将数组对象转换为集合对象
         List<String> list = Arrays.asList(array);
         System.out.println("转换后的集合对象为:" + list);
         
         //修改集合中的元素内容
         list.set(3, "4");
         System.out.println("修改后的集合对象:" + list);
         System.out.println("修改后的数组对象:" + Arrays.toString(array));

//给集合添加新元素
//list.add("five");
//System.out.println("修改后的集合对象:" + list);
//执行报错:UNsupportedOperationException
 
         System.out.println("-------------------------------------");  
         
         //对上面增删集合元素出现的问题进行处理:新建集合
         List<String> list2 = new ArrayList<String>();
         
         //集合对集合进行添加操作
         list2.addAll(list);
         System.out.println("集合2对象:" + list2);
         list2.set(2, "3");
         System.out.println("修改后的集合2对象:" + list2);
         System.out.println("集合1对象:" + list);
         System.out.println("数组对象:" + Arrays.toString(array));
         System.out.println("-------------------------------------");
         List<String> list3 = new ArrayList<String>(list);
         list3.add("five");
         System.out.println("集合3对象:" + list3);
    }
 
 }

测试结果:

 数组对象为:[one, two, three, four]
 转换后的集合对象为:[one, two, three, four]
 修改后的集合对象:[one, two, three, 4]
 修改后的数组对象:[one, two, three, 4]
 -------------------------------------
 集合2对象:[one, two, three, 4]
 修改后的集合2对象:[one, two, 3, 4]
 集合1对象:[one, two, three, 4]
 数组对象:[one, two, three, 4]
 -------------------------------------
 集合3对象:[one, two, three, 4, five]

 示意图

 

posted @ 2021-07-04 22:24  Coder_Cui  阅读(120)  评论(0编辑  收藏  举报