ArrayList常用操作

List使用:

 package com.collection.list;

 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;

 /**
  * package com.collection.list
  * functional describe:
  * ArrayList本质上是数组的封装。
  * ArrayList与LinkedList性能上的区别:
  * 随机添加或删除时:ArrayList  <  LinkedList
  * 随机获取元素时: ArrayList > LinkedList
  * 在集合末尾添加或查询元素时,性能都差不多
  *
  * @version 1.0 16-8-24 上午9:19
  * @auther luyanliang [765673481@qq.com]
  */
 public class ArrayListDemo {

     /**
      * List转换数组
      */
     public static void toArray() {
         List<String> list = createStringList();
         String[] array = list.toArray(new String[list.size()]);
         System.out.println("List转换成数组结果: " + Arrays.toString(array));
     }

     /**
      * 数组转List
      * 注:转换后的list不能进行删除或添加操作
      *
      * 运行结果:
      *   数组转换成List结果: [Lucy, Hillary Clinton, Mark]
      ×   Exception in thread "main" java.lang.UnsupportedOperationException
      *
      */
     public static void toList() {
         String[] array = new String[] {"Lucy", "Hillary Clinton", "Mark"};
         List<String> list = Arrays.asList(array);
         System.out.println("数组转换成List结果: " + list);
 //        list.remove("Lucy");
     }

     /**
      * 合并集合并去重
      * 注:Arrays.asList()创建的集合不能add()或remove()。
      * 具体原因看源码:Arrays没有重写AbstractList类的add()或者remove()方法,所以只用添加或删除都会报错
      */
     public static void addCollection() {
         List<String> original = createStringList();
         List<String> addList = new ArrayList<String>();
         addList.add("add");
         addList.add("Jone");
         addList.remove(original);
         addList.addAll(original);
         System.out.println("两个集合合并后的结果为: " + original);
     }

     /**
      * 对象引用
      *
      * 运行结果:
      *    原先集合: [Jone, Mock, Mark]
      *    引用集合: [Jone, Mock, Mark]
      *    删除元素Jone后原先集合为: [Mock, Mark]
      *    删除元素Jone后引用集合为: [Mock, Mark]
      */
     public static void copyList() {
         List<String> original = new ArrayList<String>();
         original.add("Jone");
         original.add("Mock");
         original.add("Mark");
         List<String> quote = original;
         System.out.println("原先集合: " + original);
         System.out.println("引用集合: " + quote);
         original.remove("Jone");
         System.out.println("删除元素Jone后原先集合为: " + original);
         System.out.println("删除元素Jone后引用集合为: " + quote);
     }

     /**
      * 创建集合
      * @return
      */
     public static List<String> createStringList() {
         List<String> strList = Arrays.asList("Jone", "Luce", "Mack");
         return strList;
     }

     public static void main(String[] args) {
 //        ArrayListDemo.toArray();
 //        ArrayListDemo.copyList();
 //        ArrayListDemo.toList();
         ArrayListDemo.addCollection();
     }
 }

删除List集合中指定的元素:

 import java.util.*;

 public class Test {

     public void remove1() {
         List<String> strList = this.createList();
         for (int i = 0; i < strList.size(); i ++ ) {
             String str = strList.get(i);
             if ("ab5".equals(str)) {
                 strList.remove(i);
                 i --;
             }
             System.out.println("i: " + i + ", str: " + str + ", size: " + strList.size());
         }
         System.out.println("*************");
         System.out.println(strList.toString());
     }

     public void remove2() {
         List<String> strList = this.createList();
         Iterator<String> iterator = strList.iterator();
         if (iterator.hasNext()) {
             String str = iterator.next();
             if ("ab5".equals(str)) {
                 iterator.remove();
             }
         }
     }

     public void remove3() {
         List<String> strList = this.createList();
         for (int i = 0; i < strList.size() - 1; i ++) {
             String str = strList.get(i);
             for (int j = i + 1; j < strList.size(); j ++) {
                 String nextStr = strList.get(j);
                 if (nextStr.equals(str)) {
                     strList.remove(j);
                     j --;
                 }
             }
             System.out.println("i: " + i + ", str: " + str + ", size: " + strList.size());
         }
         System.out.println("*************");
         System.out.println(strList.toString());
     }

     public static void main(String[] args) {

     }

     public void createList() {
         List<String> strList = new ArrayList<String>();
         strList.add("ab1");
         strList.add("ab2");
         strList.add("ab3");
         strList.add("ab4");
         strList.add("ab5");
         strList.add("ab4");
         strList.add("ab5");
         strList.add("ab6");
         System.out.println(strList.toString());
         System.out.println("*************");
         return strList;
     }
 }
上一篇:在服务器端如何提取checkbox提交的数据?


下一篇:用tsunami-udp加速网络传输