Java基础16-List集合

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

  • A:案例演示

    • 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
  • 思路:创建新集合方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
```java
/**
* A:案例演示
* 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
* 思路:创建新集合方式
*/
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("b");
list.add("c");
list.add("c");
list.add("c");
list.add("c");
System.out.println(list);
ArrayList newList = getSingle(list);
System.out.println(newList);
}

/*
* 去除重复
* 1,返回ArrayList
* 2,参数列表ArrayList
*/
public static ArrayList getSingle(ArrayList list) {
ArrayList newList = new ArrayList(); //创建一个新集合
Iterator it = list.iterator(); //获取迭代器
while(it.hasNext()) { //判断老集合中是否有元素
String temp = (String)it.next(); //将每一个元素临时记录住
if(!newList.contains(temp)) { //如果新集合中不包含该元素
newList.add(temp); //将该元素添加到新集合中
}
}
return newList; //将新集合返回
}
```

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

  • A:案例演示
    • 需求:ArrayList去除集合中自定义对象元素的重复值(对象的成员变量值相同)
  • B:注意事项
    • 重写equals()方法的
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      @SuppressWarnings({ "rawtypes", "unchecked" })
      public class Demo2_ArrayList {

      /**
      * * A:案例演示
      * 需求:ArrayList去除集合中自定义对象元素的重复值(对象的成员变量值相同)
      * B:注意事项
      * 重写equals()方法的
      contains方法判断是否包含,底层依赖的是equals方法
      remove方法判断是否删除,底层依赖的是equals方法
      */
      public static void main(String[] args) {
      ArrayList list = new ArrayList(); //创建集合对象
      list.add(new Person("张三", 23));
      list.add(new Person("张三", 23));
      list.add(new Person("李四", 24));
      list.add(new Person("李四", 24));
      list.add(new Person("李四", 24));
      list.add(new Person("李四", 24));

      //ArrayList newList = getSingle(list); //调用方法去除重复
      //System.out.println(newList);
      list.remove(new Person("张三", 23));
      System.out.println(list);
      }

      /*
      * 创建新集合将重复元素去掉
      * 1,明确返回值类型,返回ArrayList
      * 2,明确参数列表ArrayList
      *
      * 分析:
      * 1,创建新集合
      * 2,根据传入的集合(老集合)获取迭代器
      * 3,遍历老集合
      * 4,通过新集合判断是否包含老集合中的元素,如果包含就不添加,如果不包含就添加
      */
      public static ArrayList getSingle(ArrayList list) {
      ArrayList newList = new ArrayList<>(); //1,创建新集合
      Iterator it = list.iterator(); //2,根据传入的集合(老集合)获取迭代器

      while(it.hasNext()) { //3,遍历老集合
      Object obj = it.next(); //记录住每一个元素
      if(!newList.contains(obj)) { //如果新集合中不包含老集合中的元素
      newList.add(obj); //将该元素添加
      }
      }

      return newList;
      }
      }

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

  • A:LinkedList类概述
  • B:LinkedList类特有功能
    • public void addFirst(E e)及addLast(E e)
    • public E getFirst()及getLast()
    • public E removeFirst()及public E removeLast()
    • public E get(int index);

16.04_集合框架(栈和队列数据结构)(掌握)

    • 先进后出
  • 队列

    • 先进先出
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      public class Demo4_LinkedList {

      /**
      * 用LinkedList模拟栈结构
      */
      public static void main(String[] args) {
      //demo1();
      Stack s = new Stack();
      s.in("a"); //进栈
      s.in("b");
      s.in("c");
      s.in("d");

      while(!s.isEmpty()) { //判断栈结构是否为空
      System.out.println(s.out()); //弹栈
      }
      }

      public static void demo1() {
      LinkedList list = new LinkedList(); //创建集合对象
      list.addLast("a");
      list.addLast("b");
      list.addLast("c");
      list.addLast("d");

      /*System.out.println(list.removeLast());
      System.out.println(list.removeLast());
      System.out.println(list.removeLast());
      System.out.println(list.removeLast());*/

      while(!list.isEmpty()) {
      System.out.println(list.removeLast());
      }
      }

      }

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

  • A:案例演示

    • 需求:请用LinkedList模拟栈数据结构的集合,并测试

    • 创建一个类将Linked中的方法封装

    •   public class Stack {
            private LinkedList list = new LinkedList();        //创建LinkedList对象
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      	public void in(Object obj) {
      list.addLast(obj); //封装addLast()方法
      }

      public Object out() {
      return list.removeLast(); //封装removeLast()方法
      }

      public boolean isEmpty() {
      return list.isEmpty(); //封装isEmpty()方法
      }
      }

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

  • A:泛型概述
  • B:泛型好处
    • 提高安全性(将运行期的错误转换到编译期)
    • 省去强转的麻烦
  • C:泛型基本使用
    • <>中放的必须是引用数据类型
  • D:泛型使用注意事项
    • 前后的泛型必须一致,或者后面的泛型可以省略不写(1.7的新特性菱形泛型)
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      public class Demo1_Generic {
      public static void main(String[] args) {
      //demo1();
      //int[] arr = new byte[5]; //数组要保证前后的数据类型一致
      //ArrayList<Object> list = new ArrayList<Person>(); //集合的泛型要保证前后的数据类型一致
      //ArrayList<Object> list = new ArrayList<>(); //1.7版本的新特性,菱形泛型
      ArrayList<Object> list = new ArrayList<>(); //泛型最好不要定义成Object,没有意义
      list.add("aaa");
      list.add(true);
      }

      public static void demo1() {
      ArrayList<Person> list = new ArrayList<Person>();
      // list.add(110);
      // list.add(true);
      list.add(new Person("张三", 23));
      list.add(new Person("李四", 24));

      Iterator<Person> it = list.iterator();
      while(it.hasNext()) {
      //System.out.println(it.next());

      //System.out.println(it.next().getName() + "..." + it.next().getAge());//next方法只能调用一次,如果调用多次会将指针向后移动多次
      Person p = it.next();
      System.out.println(p.getName() + "..." + p.getAge());
      }
      }
      }

      16.07_集合框架(ArrayList存储字符串和自定义对象并遍历泛型版)(掌握)

  • A:案例演示
    • ArrayList存储字符串并遍历泛型版
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      public class Demo2_Generic {

      /**
      * * A:案例演示
      * ArrayList存储字符串并遍历泛型版
      */
      public static void main(String[] args) {
      //demo1();
      ArrayList<Person> list = new ArrayList<>();
      list.add(new Person("张三", 23));
      list.add(new Person("李四", 24));
      list.add(new Person("王五", 25));
      list.add(new Person("赵六", 26));

      Iterator<Person> it = list.iterator();
      while(it.hasNext()) {
      Person p = it.next(); //将集合中的每一个元素用Person记录
      System.out.println(p.getName() + "..." + p.getAge());
      }
      }

      public static void demo1() {
      ArrayList<String> list = new ArrayList<>(); //创建集合对象
      list.add("a");
      list.add("b");
      list.add("c");
      list.add("d");

      Iterator<String> it = list.iterator();
      while(it.hasNext()) {
      System.out.println(it.next());
      }
      }

      }

      16.08_集合框架(泛型的由来)(了解)

  • A:案例演示
    • 泛型的由来:通过Object转型问题引入
    • 早期的Object类型可以接收任意的对象类型,但是在实际的使用中,会有类型转换的问题。
      也就存在这隐患,所以Java提供了泛型来解决这个安全问题。

16.09_集合框架(泛型类的概述及使用)(了解)

  • A:泛型类概述
    • 把泛型定义在类上
  • B:定义格式
    • public class 类名<泛型类型1,…>
  • C:注意事项
    • 泛型类型必须是引用类型
  • D:案例演示
    • 泛型类的使用

16.10_集合框架(泛型方法的概述和使用)(了解)

  • A:泛型方法概述
    • 把泛型定义在方法上
  • B:定义格式
    • public <泛型类型> 返回类型 方法名(泛型类型 变量名)
  • C:案例演示
    • 泛型方法的使用

16.11_集合框架(泛型接口的概述和使用)(了解)

  • A:泛型接口概述
    • 把泛型定义在接口上
  • B:定义格式
    • public interface 接口名<泛型类型>
  • C:案例演示
    • 泛型接口的使用

16.12_集合框架(泛型高级之通配符)(了解)

  • A:泛型通配符<?>
    • 任意类型,如果没有明确,那么就是Object以及任意的Java类了
  • B:? extends E
    • 向下限定,E及其子类
  • C:? super E
    • 向上限定,E及其父类

16.13_集合框架(增强for的概述和使用)(掌握)

  • A:增强for概述
    • 简化数组和Collection集合的遍历
  • B:格式:
  •   for(元素数据类型 变量 : 数组或者Collection集合) {
          使用变量即可,该变量就是元素
      }
  • C:案例演示
    • 数组,集合存储元素用增强for遍历
  • D:好处
    • 简化遍历

16.14_集合框架(ArrayList存储字符串和自定义对象并遍历增强for版)(掌握)

  • A:案例演示

    • ArrayList存储字符串并遍历增强for版

    •   ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
      
        for(String s : list) {
            System.out.println(s);
        }

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

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

  • 迭代器,可以删除,但是必须使用迭代器自身的remove方法,否则会出现并发修改异常

  • 增强for循环不能删除

16.16_集合框架(静态导入的概述和使用)(了解)

  • A:静态导入概述
  • B:格式:
    • import static 包名….类名.方法名;
    • 可以直接导入到方法的级别
  • C:注意事项
    • 方法必须是静态的,如果有多个同名的静态方法,容易不知道使用谁?
      这个时候要使用,必须加前缀。由此可见,意义不大,所以一般不用,但是要能看懂。

16.17_集合框架(可变参数的概述和使用)(掌握)

  • A:可变参数概述
    • 定义方法的时候不知道该定义多少个参数
  • B:格式
    • 修饰符 返回值类型 方法名(数据类型… 变量名){}
  • C:注意事项:
    • 这里的变量其实是一个数组
    • 如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个

16.18_集合框架(Arrays工具类的asList()方法的使用)(掌握)

  • A:案例演示
    • Arrays工具类的asList()方法的使用
    • Collection中toArray(T[] a)泛型版的集合转数组

16.19_集合框架(集合嵌套之ArrayList嵌套ArrayList)(掌握)

  • A:案例演示
    • 集合嵌套之ArrayList嵌套ArrayList

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1210331079@qq.com

💰

Title:Java基础16-List集合

Count:2.5k

Author:千 羽

Created At:2020-06-16, 10:55:01

Updated At:2020-11-04, 15:08:19

Url:https://nateshao.github.io/2020/06/16/Java%E5%9F%BA%E7%A1%8016-List%E9%9B%86%E5%90%88/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

donation.headline

// 底部音乐
//右上角Github图标