码迷,mamicode.com
首页 > 其他好文 > 详细

ArrayList常用操作

时间:2016-08-24 19:07:34      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

 

 1 package com.collection.list;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Arrays;
 5 import java.util.List;
 6 
 7 /**
 8  * package com.collection.list
 9  * functional describe:
10  * ArrayList本质上是数组的封装。
11  * ArrayList与LinkedList性能上的区别:
12  * 随机添加或删除时:ArrayList  <  LinkedList
13  * 随机获取元素时: ArrayList > LinkedList
14  * 在集合末尾添加或查询元素时,性能都差不多
15  *
16  * @version 1.0 16-8-24 上午9:19
17  * @auther luyanliang [765673481@qq.com]
18  */
19 public class ArrayListDemo {
20 
21     /**
22      * List转换数组
23      */
24     public static void toArray() {
25         List<String> list = createStringList();
26         String[] array = list.toArray(new String[list.size()]);
27         System.out.println("List转换成数组结果: " + Arrays.toString(array));
28     }
29 
30     /**
31      * 数组转List
32      * 注:转换后的list不能进行删除或添加操作
33      *
34      * 运行结果:
35      *   数组转换成List结果: [Lucy, Hillary Clinton, Mark]
36      ×   Exception in thread "main" java.lang.UnsupportedOperationException
37      *
38      */
39     public static void toList() {
40         String[] array = new String[] {"Lucy", "Hillary Clinton", "Mark"};
41         List<String> list = Arrays.asList(array);
42         System.out.println("数组转换成List结果: " + list);
43 //        list.remove("Lucy");
44     }
45 
46     /**
47      * 合并集合并去重
48      * 注:Arrays.asList()创建的集合不能add()或remove()。
49      * 具体原因看源码:Arrays没有重写AbstractList类的add()或者remove()方法,所以只用添加或删除都会报错
50      */
51     public static void addCollection() {
52         List<String> original = createStringList();
53         List<String> addList = new ArrayList<String>();
54         addList.add("add");
55         addList.add("Jone");
56         addList.remove(original);
57         addList.addAll(original);
58         System.out.println("两个集合合并后的结果为: " + original);
59     }
60 
61     /**
62      * 对象引用
63      *
64      * 运行结果:
65      *    原先集合: [Jone, Mock, Mark]
66      *    引用集合: [Jone, Mock, Mark]
67      *    删除元素Jone后原先集合为: [Mock, Mark]
68      *    删除元素Jone后引用集合为: [Mock, Mark]
69      */
70     public static void copyList() {
71         List<String> original = new ArrayList<String>();
72         original.add("Jone");
73         original.add("Mock");
74         original.add("Mark");
75         List<String> quote = original;
76         System.out.println("原先集合: " + original);
77         System.out.println("引用集合: " + quote);
78         original.remove("Jone");
79         System.out.println("删除元素Jone后原先集合为: " + original);
80         System.out.println("删除元素Jone后引用集合为: " + quote);
81     }
82 
83     /**
84      * 创建集合
85      * @return
86      */
87     public static List<String> createStringList() {
88         List<String> strList = Arrays.asList("Jone", "Luce", "Mack");
89         return strList;
90     }
91 
92     public static void main(String[] args) {
93 //        ArrayListDemo.toArray();
94 //        ArrayListDemo.copyList();
95 //        ArrayListDemo.toList();
96         ArrayListDemo.addCollection();
97     }
98 }

 

ArrayList常用操作

标签:

原文地址:http://www.cnblogs.com/luyanliang/p/5804049.html

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