zoukankan      html  css  js  c++  java
  • Java------list用法

    定义list并将数组放入其中:

    List<Integer> temp = new ArrayList<Integer>();

    List<String> temp = new ArrayList<String>();

    获得集合内元素个数:list.size();

    添加元素:
    默认添加:list.add(e);
    指定下标添加(添加后下标后的元素向后挪一位):list.add(index,e);

    如果我们得到的List还想要增删,那么可以用addAll方法

           String[] str = {"1", "2", "3", };
           List<String> strArray = new ArrayList<String>();
           strArray.addAll(Arrays.asList(str));
           strArray.add("a");
         
           System.out.println(strArray);

           结果为:1 2 3 a

    删除元素:
    返回是否删除:list.remove(e);
    直接删除指定下标的元素(只删除找到的第一个相符合的元素):list.remove(index);

    替换元素(替换掉指定下标的元素):list.set(index,e);

    取出元素:list.get(index);

    清空集合:list.clear();

    判断集合中是否存在某个元素(存在返回true,不存在返回false):list.contains(e);

    对比两个集合中的所有元素:
    两个对象一定相等:list.equals(list2);
    两个对象不一定相等:list.hashCode() == list2.hashCode();
    (两个相等对象的equals方法一定为true, 但两个hashcode相等的对象不一定是相等的对象。)

    获得元素下标:
    元素存在则返回找到的第一个元素的下标,不存在则返回-1:list.indexOf(e);
    元素存在则返回找到的最后一个元素的下标,不存在则返回-1:list.lastIndexOf(e);

    判断集合是否为空(空则返回true,非空则返回false):list.isEmpty();

    返回Iterator集合对象:list.iterator();

    将集合转换为字符串:list.toString();

    截取集合(从fromIndex开始在toIndex前结束,[fromIndex,toIndex)):list.subList(fromIndex,toIndex);

    将集合转换为数组:
    默认类型:list.toArray();

  • 相关阅读:
    Project Chameleon Work In Progress 14
    All about Project Chameleon
    网页中图片连续滚动代码 (转)
    一点感言
    一些常用javascript代码(转)
    asp.net(c#)的一个非常非常奇怪的问题
    用javascript拦截a标签的超链接执行
    asp.net中用TreeView控件实现无限分级的好办法
    windows7 安装ez usb基本驱动
    管道编程
  • 原文地址:https://www.cnblogs.com/610553824lyx/p/6648929.html
Copyright © 2011-2022 走看看