zoukankan      html  css  js  c++  java
  • List中的常用方法:

    List常用的方法(2)

    boolean:       list.remove(Object o)

    作用:移除列表中首个出现的指定元素。默认删除对象的equals方法 ,使用时先看看equals方法有

    没有重写

    Object obj :  list.remove(int index)

    作用:根据给出的下标删除列表中指定位置的元素

    Object obj:    list.set(index, element) 

    作用:用指定元素替代列表中指定位置的元素,返回被改变的元素的值。

    int:            list.indexOf(Object o)

    作用:返回列表中首次出现指定元素的索引,如果不包含指定元素则返回-1  ,默认调用equals方法。

    Object obj:    list.get(index)

    作用:返回列表中指定位置的元素

    boolean :      list.contains(Object o)

    作用: 判断列表中是否包含指定元素,如果是返回true,否则false;

    该方法默认的也是调用的系统equals方法,因此使用前也应该根据需要重写equals方法

    List常用的方法(3): 集合与集合之间的操作

    boolean :  list.addAll(Collection col):

    作用:追加指定col集合中的元素到此列表的结尾。

       boolean : list.addAll(int index,Collection col)

    作用:将指定col中的所有元素全部插入的列表中的指定位置。  

    boolean: list.removeAll(Collection col):

    作用:  从列表中移除指定集合中包含的所有元素。

    boolean :retainAll(Collection col):

    作用:   保留与另一个集合中相同的元素,其余的删除

    public class MethodOfList {
    
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
    
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("e");
            list.add("f");
            list.add("g");
            
            //boolean: list.remove(Object obj)
            boolean b = list.remove("g");  //根据equals()方法,删除列表中首个出现的指定元素
            System.out.println(b);  //true
            System.out.println(list);// [a, b, c, d, e, f]
    
            //Object obj:  list.remove(int index)
            String s = list.remove(5);
            System.out.println(s+"---"+list);  //f---[a, b, c, d, e]
    
            //Object obj:  list.set(int index,E element)
            System.out.print(list+"---");
            String s1 = list.set(3, "change");
            System.out.println(s1+"---"+list);    //[a, b, c, d, e]---d---[a, b, c, change, e]
    
            //int : list.indexOf(Object obj)
            int index = list.indexOf("change");
            System.out.println(index);  //3
            
            // Object obj: list.get(index)
            String s2 = list.get(3);
            System.out.println(s2);   //change
            
            //boolean: list.contains(Object obj)
            boolean flag = list.contains("d");
            System.out.println(flag);  //false
            /*
             * 集合与集合之间的操作
             */
            List<String> list1 = new ArrayList<String>();
            list1.add("m");
            list1.add("n");
            list1.add("o");
            list1.add("p");
            //boolean : list.addAll(Collection col)
            boolean b1 = list.addAll(list1);
            System.out.println(b1+"---"+list); //true---[a, b, c, change, e, m, n, o, p]
    
    
            //boolean :list.addAll(int index , Collection col)
            boolean b2 = list.addAll(0, list1);
            System.out.println(b2+"---"+list);  //true---[m, n, o, p, a, b, c, change, e, m, n, o, p]
    
    //        //boolean : list.removeAll(Collection col) 
    //        boolean b3 = list.removeAll(list1);
    //        System.out.println(b3+"---"+list);  //true---[a, b, c, change, e]
    
            //boolean : list.retainAll()
            boolean b4 = list.retainAll(list1);
            System.out.println(b4+"---"+list);//true---[m, n, o, p, m, n, o, p]
    
            
        }
    }
  • 相关阅读:
    debounce
    react-xiguan
    备忘录
    ie导出问题
    umi 动态路由配置
    tsconfig
    关于vue 和react 中的hash与锚点冲突问题
    lodash
    pyplot绘图
    Numpy实现图像变换
  • 原文地址:https://www.cnblogs.com/iwwenbo/p/3351325.html
Copyright © 2011-2022 走看看