• java——集合——List集合——List集合


     

     

     

     

     

    List集合的概述与方法使用

    java.util.List接口 extends Collection接口
    List接口的特点:
    1.有序的集合,存储元素和取出元素的顺序是一致的(存储123 取出123)
    2.有索引,包含了一些带索引的方法
    3.允许存储重复的元素

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

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

    注意:
    操作索引的时候,一定要防止索引越界异常

    • IndexOutOfBoundsException:索引越界异常,集合会报
    • ArrayIndexOutOfBoundsException:数组索引越界异常
    • StringIndexOutOfBoundsException:字符串索引越界异常

    public class Demo01List {
        public static void main(String[] args) {
            //创建一个List集合对象,多态
            List<String> list = new ArrayList<>();
            //使用add方法往集合中添加元素
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("a");
            //打印集合
            System.out.println(list);//[a, b, c, d, a]  不是地址重写了toString
    
            //public void add(int index, E element): 将指定的元素,添加到该集合中的指定位置上。
            //在c和d之间添加一个itheima
            list.add(3,"itheima");//[a, b, c, itheima, d, a]
            System.out.println(list);
    
            //public E remove(int index): 移除列表中指定位置的元素, 返回的是被移除的元素。
            //移除元素
            String removeE = list.remove(2);
            System.out.println("被移除的元素:"+removeE);//被移除的元素:c
            System.out.println(list);//[a, b, itheima, d, a]
    
            //public E set(int index, E element):用指定元素替换集合中指定位置的元素,返回值的更新前的元素。
            //把最后一个a,替换为A
            String setE = list.set(4, "A");
            System.out.println("被替换的元素:"+setE);//被替换的元素:a
            System.out.println(list);//[a, b, itheima, d, A]
    
            //List集合遍历有3种方式
            //使用普通的for循环
            for(int i=0; i<list.size(); i++){
                //public E get(int index):返回集合中指定位置的元素。
                String s = list.get(i);
                System.out.println(s);
            }
            System.out.println("-----------------");
            //使用迭代器
            Iterator<String> it = list.iterator();
            while(it.hasNext()){
                String s = it.next();
                System.out.println(s);
  • 相关阅读:
    CF991D Bishwock
    CF1010D Mars rover
    NOIP 2011 计算系数
    SDOI 2008 仪仗队
    浅谈欧拉函数
    CF1249F Maximum Weight Subset
    NOIP 2011 铺地毯
    CF707D Persistent Bookcase
    C++ STL bitset 容器详解
    CF798D Mike and distribution
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/16906364.html
Copyright © 2020-2023  润新知