- package com.it.collection;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Vector;
- /*
- * ArrayList:底层数据结构是数组,查询快,增删慢;线程不安全,效率高
- * Vector:底层数据结构是数组,查询快,增删慢;线程安全,效率低
- * LinkedList:底层数据结构是链表,查询慢,增删快,线程不安全,效率高
- */
- public class CollectionDemo {
- public static void main(String[] args) {
- /*
- * ArrayList
- */
- ArrayList c = new ArrayList();
-
- c.add("hello");
- c.add("sina");
- c.add("weibo");
- //迭代器
- Iterator i = c.iterator();
- while (i.hasNext()) {
- String s = (String) i.next();
- System.out.println(s);
- }
- System.out.println("---");
- //普通for
- for (int x = 0; x < c.size(); x++) {
- String s = (String) c.get(x);
- System.out.println(s);
- }
- System.out.println("-----");
- /*
- * Vector
- */
- Vector v = new Vector();
- v.add("hello");
- v.add("sina");
- v.add("weibo");
- Iterator i1 = v.iterator();
- while (i1.hasNext()) {
- String s1 = (String) i1.next();
- System.out.println(s1);
- }
- System.out.println("---");
- for (int x = 0; x < v.size(); x++) {
- String s1 = (String) v.get(x);
- System.out.println(s1);
- }
-
- System.out.println("-----");
- /*
- * LinkedList
- */
- LinkedList l = new LinkedList();
- l.add("hello");
- l.add("sina");
- l.add("weibo");
- Iterator i2 = l.iterator();
- while (i2.hasNext()) {
- String s2 = (String) i2.next();
- System.out.println(s2);
- }
- System.out.println("---");
- for (int x = 0; x < l.size(); x++) {
- String s2 = (String) l.get(x);
- System.out.println(s2);
- }
- }
- }
复制代码 |
|