码迷,mamicode.com
首页 > 编程语言 > 详细

Java------list用法

时间:2017-03-30 22:26:45      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:方法   min   存在   false   tor   hashcode   size   string   结束   

定义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();

 

Java------list用法

标签:方法   min   存在   false   tor   hashcode   size   string   结束   

原文地址:http://www.cnblogs.com/610553824lyx/p/6648929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!