• Java List集合的介绍与常用方法


    List接口的介绍

    List接口简介:
    java.util.List接口继承自Collection接口,是单列集合的一个重要分支,习惯性地会将实现了List接口的对象称为List集合。
    在List集合中允许出现重复的元素,所有的元素是以一种线性方式进行存储的,在程序中可以通过索引来访可集合中的指定元素。另
    外,List集合还有一个特点就是元素有序,即元素的存入顺序和取出顺序一致

    List接口的特点

    1.
    它是一个元素存取有序的集合。例如,存元素的顺序是11、22、33。那么集合中,元素的存储就是按照11、22、33的顺序完成的。
    
    2.
    它是一个带有索引的集合,通过索引就可以精确的操作集合中的元素(与数组的索引是一个道理)。
    
    3.
    集合中可以有重复的元素,通过元素的 equals方法,来比较是否为重复的元素。

    List接口中带索引的(特有)方法

    // 1、将指定的元素,添加到该集合中的指定位置上。
    public void add(int index, E element)
    
    // 2、返回集合中指定位置的元素。
    public E get(int index)
    
    // 3、移除列表中指定位置的元素,返回的是被移除的元素。
    public E remove(int index)
    
    // 4、用指定元素替换集合中指定位置的元素,返回值的更新前的元素。
    public E set(int index, E element)

    add()方法

    说明:hasNext()方法,获取迭代器是否含有下一个元素(含有就返回true)

             next()方法,获取迭代器下一个元素

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class DemoListAdd {
        public static void main(String[] args) {
            // 创建集合对象
            List<String> arrayList = new ArrayList<>();
    
            // 往集合的指定位置上添加给定的元素
            arrayList.add(0, "Index 0 元素");
            arrayList.add(1, "Index 1 元素");
            arrayList.add(2, "Index 2 元素");
    
            // 遍历集合,查看结果
            // 获取迭代器对象
            Iterator<String> ite = arrayList.iterator();
            // 输出
            while (ite.hasNext()) {
                System.out.println(
                        ite.next()
                );
            }
        }
    }
    输出结果:
    Index 0 元素
    Index 1 元素
    Index 2 元素

    get()方法

    import java.util.ArrayList;
    import java.util.List;
    
    public class DemoListGet {
        public static void main(String[] args) {
            // 创建集合对象
            List<String> arrayList = new ArrayList<>();
    
            // 往集合的指定位置上添加给定的元素
            arrayList.add(0, "Index 0 元素");
            arrayList.add(1, "Index 1 元素");
            arrayList.add(2, "Index 2 元素");
    
            // 获取指定位置中集合的元素
            String index0 = arrayList.get(0);
            String index1 = arrayList.get(1);
            String index2 = arrayList.get(2);
    
            // 输出
            System.out.println("索引0处的元素:" + index0);
            System.out.println("索引1处的元素:" + index1);
            System.out.println("索引2处的元素:" + index2);
        }
    }
    输出结果:
    索引0处的元素:Index 0 元素
    索引1处的元素:Index 1 元素
    索引2处的元素:Index 2 元素

    remove()方法

    public class DemoListRemove {
        public static void main(String[] args) {
            // 创建集合对象
            List<String> arrayList = new ArrayList<>();
    
            // 往集合的指定位置上添加给定的元素
            arrayList.add(0, "元素0");
            arrayList.add(1, "元素1");
            arrayList.add(2, "元素2");
            // 查看集合
            System.out.println("移除元素前:" + arrayList);
    
            // 删除集合中的部分元素
            arrayList.remove(1);
            System.out.println("移除元素1后:" + arrayList);
        }
    }
    输出结果:
    移除元素前:[元素0, 元素1, 元素2]
    移除元素1后:[元素0, 元素2]

    注意:移除一个元素以后,在被移除元素的后面的每个元素索引减1

    set()方法

    import java.util.ArrayList;
    import java.util.List;
    
    public class DemoListSet {
        public static void main(String[] args) {
            // 创建集合对象
            List<String> arrayList = new ArrayList<>();
    
            // 往集合的指定位置上添加给定的元素
            arrayList.add(0, "原始元素0");
            arrayList.add(1, "原始元素1");
            arrayList.add(2, "原始元素2");
            // 查看集合
            System.out.println("集合被替换元素前:" + arrayList);
    
            // set方法替换指定位置的元素
            arrayList.set(0, "替换元素0");
            System.out.println("集合被替换元素后:" + arrayList);
        }
    }
    集合被替换元素前:[原始元素0, 原始元素1, 原始元素2]
    集合被替换元素后:[替换元素0, 原始元素1, 原始元素2]
  • 相关阅读:
    redis+nginx+tomcat故障大全
    Nginx+Tomcat+Redis实现负载均衡、资源分离、session共享
    Redis初步认识
    私有IP地址共有三个范围段
    公有云和私有云的区别
    svn安装与其服务器搭建
    datagrid后台分页js
    easyui.dialog.js
    EasyUI 另一种form提交方式
    easyui- grid前台分页
  • 原文地址:https://www.cnblogs.com/liyihua/p/12192254.html
Copyright © 2020-2023  润新知