Thursday, September 27, 2007

(ZT)

 

ArrayList VS Vector VS Stack

ArrayList可用Iterator遍历,但Vector可用Iterator或者Enumeration遍历。两者的实现算法相差不多。但差别最重要的一点是线程安全问题Vector类的所有方法都是线程同步的,两个线程并发访问Vector对象将是安全的,但只有一个线程访问Vector对象时,因为源程序仍调用了同步方法,需要额外的监视器检查,运行效率要低些。ArrayList类中的所有方法是非同步的,所以在没有线程安全问题时,最好用ArrayList,程序的效率会高些。在有线程安全问题时,且我们的程序又没有自己处理(自己处理是指对调用ArrayList的代码或方法加上同步代码同步处理)时,只能用Vector。Stack是Vector的继承,但主要用到的方法是push and pop and empty,不然也就失去了Stack的意义,Stack也是线程安全的。

6.ArrayList VS LinkedList

ArrayList是用数组实现的,可以随即访问每个元素,不用考虑性能问题。LinkedList是使用节点彼此连接实现的,要访问指定元素,必须遍历节点实现定位。ArrayList的缺点是在中间添加元素时,后面的元素必须后移,而LinkedList则通过节点连接来实现,只须修改节点的前后引用即可。但在中间插入(删除)元素的情况较少见,所以LinkedList比较少用

 

Array vs ArrayList vs LinkedList vs Vector with very good overview and examples.

An ArrayList is ordered collection (also known as a sequence). It means that the user of controls where each element is inserted. This class is very similar to the Vector class, excepting that it is not synchronized. It must be synchronized externally.

An ArrayList is better than Array to use when you have no knowledge in advance about elements number. ArrayList are slower than Arrays. So, if you need efficiency try to use Arrays if possible.

Short Overview on main features of ArrayList:

  • - The add operation runs O(n) time.
  • - The isEmpty, size, iterator, set, get and listIterator operations require the same amount of time, independently of element you access.
  • - Only Objects can be added to an ArrayList
  • - Implements all methods from the List interface
  • - Permits null elements

An ArrayList could be a good choice if you need very flexible Array type of collection with limited functionality.

One small remark for those who needs faster ArrayList. ArrayLists internally implemented as an Array. So when run an "add" command a new Array will be created with n+1 dimension. All "older" elements will be copied to first n elements and last n+1 one will filled with the value which you provide with add() method. To avoid that internal re-copying of Arrays you should use ensureCapacity(int requestCapacity) method - it will create an array only once.

If you have higher requirements on number of accessible methods for your Array like collection instead of ArrayList you have better choice - LinkedList.

LinkedList is much more flexible and lets you insert, add and remove elements from both sides of your collection - it can be used as queue and even double-ended queue!

Internally a LinkedList does not use arrays, it is much more modern! LinkedList is a sequence of nodes, which are double linked. Each node contains header, where actually objects are stored, and two links or pointers to next or previous node. A LinkedList looks like a chain, consisting of people who hold each other's hand. You can insert people or node into that chain or remove. Linked lists permit node insert/remove operation at any point in the list in constant time.

LinkedList is actually luxury extension of an ArrayList: it gives you many methods which you usually implement yourself around of different type of collections.

For example if you need to add new element to the end of an ArrayList you have to look at the size of the collection and then add that new element at n+1 place. In LinkedList you do it directly by addLast(E e) method.

Another example. Depending on your expectations you can chose either getFirst/getLast or  peekFirst/peekLast methods. Last "peek" methods are completelly new sort of methods and introduced in Java 1.6.

They slightly differs from set/get methods - they do not throw any kind of exceptions if this list is empty, return just null.

New "poll" methods - pollFirst and pollLast combine two methods: get and remove an element from your collection. For example pollFirst() method gets and removes the first element from this list. This method does throw any kind of exception like IndexOutOfBoundsException in case of ArrayList, instead it just returns null (if this list is empty).

Because the LinkedList implements queue interface you can pop and push new elements from/into your collection.

If you created capacity restricted collection you can "examine" the result of an operation by using offerFirst/offerLast methods. They are also new (since Java 1.6) and return boolean result on the operations instead of throwing IllegalStateException as addFirst/addLast methods do. In case if possible to insert an element at the beginning/end of collection you you get "true" result.

Look at the code snippet - Usage of LinkedList:

Code:


List listA = new LinkedList();
        List listB = new LinkedList();
        for (int i = 0; i < MAX; i++) {
            System.out.println("  - Storing Integer(" + i + ")");
            listA.add(new Integer(i));
        }
        System.out.println("  - Storing String(Alex)");
        listA.add("Alex");
        System.out.println("  - Storing String(Melody)");
        listA.add("Melody");
        System.out.println("  - Storing String(Jeff)");
        listA.add("Jeff");
        System.out.println("  - Storing String(Alex)");
        listA.add("Alex");
        .............................................
        .............................................
        .............................................

0 Comments:

Post a Comment

<< Home