ArrayList or. LinkedList or. Vecto

ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array.

LinkedList is implemented as a double linked list. Its performance on add and remove is better than Arraylist, but worse on get and set methods.

Vector is similar with ArrayList, but it is synchronized.

ArrayList is a better choice if your program is thread-safe. Vector and ArrayList require more space as more elements are added. Vector each time doubles its array size, while ArrayList grow 50% of its size each time. LinkedList, however, also implements Queue interface which adds more methods than ArrayList and Vector, such as offer(), peek(), poll(), etc.

ArrayList example

 1 ArrayList<Integer> al = new ArrayList<Integer>();
 2 al.add(3);
 3 al.add(2);        
 4 al.add(1);
 5 al.add(4);
 6 al.add(5);
 7 al.add(6);
 8 al.add(6);
 9  
10 Iterator<Integer> iter1 = al.iterator();
11 while(iter1.hasNext()){
12     System.out.println(iter1.next());
13 }

LinkedList example

 1 LinkedList<Integer> ll = new LinkedList<Integer>();
 2 ll.add(3);
 3 ll.add(2);        
 4 ll.add(1);
 5 ll.add(4);
 6 ll.add(5);
 7 ll.add(6);
 8 ll.add(6);
 9  
10 Iterator<Integer> iter2 = ll.iterator();
11 while(iter2.hasNext()){
12     System.out.println(iter2.next());
13 }

Note: The default initial capacity of an ArrayList is pretty small. It is a good habit to construct the ArrayList with a higher initial capacity. This can avoid the resizing cost.

转载于:https://www.cnblogs.com/slowd/p/4584498.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值