1. 1 ///: JavaBasic//com.cnblogs.pattywgm.day1//CollectionTest.java
  2. 2
  3. 3 package com.cnblogs.pattywgm.day1;
  4. 4
  5. 5 import java.io.BufferedReader;
  6. 6 import java.io.IOException;
  7. 7 import java.io.InputStreamReader;
  8. 8 import java.util.ArrayList;
  9. 9 import java.util.Arrays;
  10. 10 import java.util.Iterator;
  11. 11 import java.util.List;
  12. 12 import java.util.ListIterator;
  13. 13
  14. 14 /**
  15. 15 * @author Administrator
  16. 16 * @Time: 2014-6-13
  17. 17 * @Descri: CollectionTest.java
  18. 18 */
  19. 19
  20. 20 public class CollectionTest {
  21. 21 //Testing of List
  22. 22 List<String> list1=new ArrayList<String>();
  23. 23 List<String> list2=new ArrayList<String>();
  24. 24 List<Integer> list3=new ArrayList<Integer>();
  25. 25
  26. 26 public CollectionTest() {
  27. 27 addElement();
  28. 28 }
  29. 29
  30. 30 public void addElement(){
  31. 31 int count=5;
  32. 32 while(count>=0){
  33. 33 //Appends the specified element to the end of list1
  34. 34 list1.add("goods"+count);
  35. 35 /*Insert the specified element in the head of list1
  36. 36 list1.add(0, "goods"+count);
  37. 37 */
  38. 38 count--;
  39. 39 }
  40. 40
  41. 41 }
  42. 42
  43. 43 public void addCollectionElements(){
  44. 44 //Appends all of the elements in list1 (the specified collection) to the end of list2
  45. 45 list2.addAll(list1);
  46. 46 }
  47. 47
  48. 48 public void getElement(List<String> list){
  49. 49 // 1)
  50. 50 for(int i=0;i<list.size();i++){
  51. 51 System.out.printf("%s's %dth element is %s",list,i,list.get(i).toString());
  52. 52 System.out.println();
  53. 53 }
  54. 54 System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
  55. 55 // 2)use Iterator
  56. 56 Iterator<String> iter=list.iterator();
  57. 57 int j=0;
  58. 58 while(iter.hasNext()){
  59. 59 System.out.printf("%s's %dth element is %s",list,j++,iter.next());
  60. 60 System.out.println();
  61. 61 }
  62. 62 }
  63. 63
  64. 64 public void removeElement(List<String> list,int index,String obj){
  65. 65 // 1) Removes the element at the specified position in this list
  66. 66 list.remove(index);
  67. 67 // 2) Removes the first occurrence of the specified element from this list, if it is present .
  68. 68 list.remove(obj);
  69. 69 /*
  70. 70 * Removes all of the elements from this list.
  71. 71 * The list will be empty after this call returns.
  72. 72 */
  73. 73 // list.clear();
  74. 74
  75. 75 }
  76. 76
  77. 77 public List<String> getSubList(List<String> list, int fromIndex, int toIndex){
  78. 78 return list.subList(fromIndex, toIndex);
  79. 79 }
  80. 80 //ListIterator
  81. 81 public void listIter(List<String> list){
  82. 82 ListIterator<String> listIter= list.listIterator();
  83. 83 //The element is inserted immediately before the next element that would be returned by next
  84. 84 //here the nexindex is 0
  85. 85 listIter.add("goods end");
  86. 86 System.out.println("previous index is :"+listIter.previousIndex()+" "
  87. 87 +"previous element is "+listIter.previous());
  88. 88 System.out.println("Change the last element of list...");
  89. 89 listIter.set("goods end changed");
  90. 90 System.out.println("正向遍历Starting...");
  91. 91 while(listIter.hasNext()){
  92. 92 System.out.println(listIter.next());
  93. 93 }
  94. 94 System.out.println("反向遍历Starting...");
  95. 95 while(listIter.hasPrevious()){
  96. 96 System.out.println(listIter.previous());
  97. 97 }
  98. 98 //Removes from the list the last element that was returned by next or previous
  99. 99 //Becareful: just one element every call
  100. 100 listIter.remove();
  101. 101 }
  102. 102
  103. 103 //add element : stdin
  104. 104 public void addElementStandard(List<Integer> list){
  105. 105 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  106. 106 try {
  107. 107 String str[]=br.readLine().split(",");//split element with ','
  108. 108 for(String ss:str){
  109. 109 list.add(Integer.parseInt(ss));
  110. 110 }
  111. 111 } catch (IOException e) {
  112. 112 System.out.println("Error happended!!!");
  113. 113 e.printStackTrace();
  114. 114 }
  115. 115 }
  116. 116
  117. 117 public void sortList(List<Integer> list){
  118. 118 Object[] sortedList=list.toArray();
  119. 119 Arrays.sort(sortedList);
  120. 120
  121. 121 for(Object obj:sortedList){
  122. 122 System.out.print(obj+" ");
  123. 123 }
  124. 124 }
  125. 125 }
  126. 126 //:~

以上代码是对Java容器中List接口方法应用的各种实现,包括列表元素的增删改查。下面逐一做详细介绍:

1、增加列表元素

向列表中增加元素,总体来说有两类,即使用add()或addAll(),前者是向列表中直接插入指定的单个元素,而后者则是添加指定 collection 中的所有元素到此列表。

此外,可以指定向列表中插入元素的位置,这就将add()又细分为:add(E e)  add(int index, E element),前者默认在列表尾部增加元素,后者则在指定位置插入元素。

<addAll()的划分同add()>

2、删除列表元素

  Java API中,关于列表的删除操作有:remove()、removeAll()以及clear(),clear()将同时删除列表中的所有元素,但列表本身还是存在的,remove()依

据参数类型的不同,既可以实现删除指定位置的元素,也可以在不知道元素位置而知道列表中包含此元素时实现删除指定的元素,但此时只是删除第一次出现的指定元素,若

想删除列表中所有与指定元素相同的元素,可以循环调用remove(Object obj)直到列表中已不存在该元素。removeAll()则是实现从列表中移除指定 collection 中包含的

所有元素示例如下:

  

  1. List<String> lis=new ArrayList<String>();
  2. lis.add("one");
  3. lis.add("two");
  4. lis.add("three");
  5. lis.add("one");
  6. lis.add("four");
  7. lis.add("one");
  8. lis.remove("one");
  9. for(String s:lis){
  10. System.out.println(s);
  11. }
  12. System.out.println("~~~~~~~~~~~~~~~");
  13. while(lis.contains("one")){
  14. lis.remove("one");
  15. }
  16. for(String s:lis){
  17. System.out.println(s);
  18. }

3、改变列表中已有元素

  改变列表中已有元素,主要通过set()方法实现,该方法包含两个参数,实现用指定元素(参数2)替换列表中指定位置(参数1)的元素。

4、查找列表元素

  同增删操作类似,查找也分为contains()和cotainsAll()两类,在此不再赘述。

【说明:】

  List接口所提供的还有其他很多方法,本文只说明了方法名称,未详细表明方法参数,具体请参照JAVA API。

Java 容器:Collection 初探之 List的更多相关文章

  1. java容器-Collection

    1.介绍    collection<E>是java中容器的最主要的接口,该接口继承于Iterable<E>,使得java中所有实现Collection<E>的容器 ...

  2. java容器——Collection接口

    Collection是Set,List接口的父类接口,用于存储集合类型的数据. 2.方法 int size():返回集合的长度 void clear():清除集合里的所有元素,将集合长度变为0 Ite ...

  3. Java容器---Collection接口中的共有方法

    1.Collection 接口 (1)Collection的超级接口是Iterable (2)Collection常用的子对象有:Map.List.Set.Queue. 右图中实现黑框的ArrayLi ...

  4. java容器collection的一些简单特点

    1.List ArrayList 可随机访问元素,但中间插入和一处元素较慢 LinkedList 在中间进行的插入和删除操作代价较小,随机访问比ArrayList较慢 特性集比ArrayList大 2 ...

  5. 【Java心得总结六】Java容器中——Collection

    在[Java心得总结五]Java容器上——容器初探这篇博文中,我对Java容器类库从一个整体的偏向于宏观的角度初步认识了Java容器类库.而在这篇博文中,我想着重对容器类库中的Collection容器 ...

  6. 【Java心得总结五】Java容器上——容器初探

    在数学中我们有集合的概念,所谓的一个集合,就是将数个对象归类而分成为一个或数个形态各异的大小整体. 一般来讲,集合是具有某种特性的事物的整体,或是一些确认对象的汇集.构成集合的事物或对象称作元素或是成 ...

  7. java中Collection容器

    1.容器(Collection)也称为集合, 在java中就是指对象的集合. 容器里存放的都只能是对象. 实际上是存放对象的指针(头部地址): 这里对于八种基本数据类型,在集合中实际存的是对应的包装类 ...

  8. 理解java容器:iterator与collection,容器的起源

    关于容器 iterator与collection:容器的起源 iterator的简要介绍 iterable<T> iterator<T> 关于remove方法 Collecti ...

  9. java容器的理解(collection)

    容器类(Conllection)对于一个开发者来说是最强大的工具之一,可以大幅提高编程能力.容器是一个将多个元素组合到一个单元的对象,是代表一组对象的对象,容器中的对象成为它的元素. 容器适用于处理各 ...

随机推荐

  1. Nginx php-fpm php mysql

    rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm ...

  2. 是不是content-type: text/html的数据包一到,浏览器就肯定刷新页面?

    整理自:http://q.cnblogs.com/q/54726/ 是不是content-type: text/html的数据包一到,浏览器就肯定刷新页面? 或者说,浏览器收到的状态正常的conten ...

  3. C# 两个集合对比获取不同

    public class CompareCollection { public List<string> CompareList(List<string> oldList, L ...

  4. 【freemaker】之include,import区别

    新建三个模版文件 inc1.ftl,inc2.ftl,03.ftml <#--inc1--> <#assign username="老李1"> <#- ...

  5. C#和Javascript间互转的Xxtea加解密

    很有意思的一件事情,当我想要找 Xxtea 加解密算法的时候,发现了前同事(likui318)的代码,不妨分享出来.此代码满足: 1:Xxtea支持中文: 2:支持 JS 和 C# 加解密之间的互转: ...

  6. [jQuery]html(),text(),val()方法的区别

    1.HTML html():取得第一个匹配元素的html内容.这个函数不能用于XML文档.但可以用于XHTML文档 html(val):设置每一个匹配元素的html内容.这个函数不能用于XML文档.但 ...

  7. A*算法入门

    来源:http://www.cppblog.com/mythit/archive/2009/04/19/80492.aspx 在看下面这篇文章之前,先介绍几个理论知识,有助于理解A*算法. 启发式搜索 ...

  8. myEclipse

    破解myEclipse 建立一个java项目,将reg.java放入,并且运行在控制台 输入账户 回车就会出现 序列号 菜单栏--->myeclipse-->substription in ...

  9. Release时error c1083 无法打开包括文件

    Release时error c1083 无法打开包括文件, 但Debug时没事. 项目里面包含了其实项目的头文件, 头文件目录就放在项目下面, 这个头文件里面调用其它头文件, 采用的是<xx/y ...

  10. Python从题目中学习:range()和xrange()

    近期给公司培训Python,好好啃了啃书本,查了查资料,总结一些知识点. --------------------------------------------------------------- ...