青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

copied from Pankaj Kumar's Weblog


The trade-offs in using java.util.ArrayList and java.util.LinkedList should be straight-forward, shouldn't it? At least this is what I used to think till today. But then most of my thinking around these datastructures were formed during college days, when C was the hottest language and Java and its Collection classes just didn't exist.

Not surprisingly, it is natural for me to think of arrays as fixed size containers, where elements can be accessed at random location through O(1) operation (ie; in constant time) and insertion/deletion in the middle are O(N) operations (ie; could take time proportional to the size of the array) and hence, are better avoided. In contrast, a linked list can grow in size, access of its head or tail and insertion/deletion in the middle are all O(1) operations (assuming that you have pointer to an adjacent element).

It is possible get around the fixed size limitation of arrays by writing a wrapper which will allocate a new array, copy the elements of the old array into the new one and then discard the old one (BTW, this is what ArrayList does). Still, the basic arrays remain a datastructure for collections of fixed size. In contrast, a linked list consists of nodes with 'pointers' to the next and previous node in the list. So, adding or removing a node is simply a matter of reassigning the pointers. Of course, this implies linear time for traversing upto an indexed node, starting from beginning or end. This simple model is very handy in deciding when to use an array and when to use a linked list.

In Java, the ArrayList and LinkedList classes provide a uniform interface to both these datastructures and hence, destroy this simple conceptual model, so necessary to make judicious implementation decisions, in impressionable young minds of many Java programmers. Let me further elaborate this with my recent own experience.

Today, while going over a graph traversal code, I was somewhat alarmed by the generous use of ArrayLists. This code was written by someone who perhaps had learnt programming with Java. As hinted earlier, both ArrayList and LinkedList implement List interface and support similar operations. An ArrayList can grow dynamically and allows insertion/deletion of elements. A LinkedList also allows access of elements through an index, exactly the same way as an ArrayList. This is all fine. However, the problem is that the apparent similarity in the API hides the widely different memory and time costs of these datastructures for different kinds of operations, luring the unwary to use them in dangerous ways:

  1. An empty ArrayList takes two to three times more memory than an empty LinkedList (because ArrayList would typically preallocate memory). This becomes important if you plan to keep an adjacency list of nodes for each node in the graph and you know beforehand that the nodes will have at most one or two adjacent nodes and the total number of nodes in the graph can be quite large.

  2. The following straight-forward loop to iterate over all elements of a list


    ??for (int i = 0; i < list.size(); i++)
    ????doSomething(list.get(i));


    works great for an ArrayList but will cause serious performance problems for a LinkedList. Can you guess why? The right way to iterate over a LinkedList is:


    ??ListIterator li = list.listIterator(0);
    ??while (li.hasNext())
    ????doSomething(li.next());


  3. Although both ArrayList and LinkedList allow insertion/deletion in the middle through similar operations (ie; by invoking add(int index) or remove(index)), these operations do not offer you the advantage of O(1) insertion/deletion for a LinkedList. For that, you must work through a ListIterator.

While researching on this topic, I did find a couple of good articles on the Web:


  • JDC Tech Tip article on Using ArrayList/LinkedList. Good coverage of the topic. Worth reading if you want to know more about performance tradeoffs.
  • joustlog entry titled LinkedList vs. ArrayList performance tests and subsequent clarification. This entry is more focussed in scope, pointing out the fact that addition as the end is faster for ArrayList than for LinkedList. The only thing I would like to add is that addition at the end of a LinkedList is always O(1) whereas addition at the end of an ArrayList is amortized O(1), meaning if you do M at-the-end additions then the total cost will be proportional to M. This is due to the fact that the underlying array may have to be grown (a new one to be allocated, old one to be copied and discarded) when the capacity is reached. However, I can understand that a normal at-the-end addition (ie; not involving resizing of the underlying array) will be faster for ArrayList (compared to LinkedList).

I am not advocating either ArrayList or LinkedList, though it can be justifiably argued that the use of ArrayList is better suited in many more programming scenarios, and I have no contention with that. The point I am making is that the sameness of the API makes it easy for programmers to assume that these can be used interchangeably. Nothing can be farther from truth. They are distinct datastructures, each optimized for certain kinds of operations and domain of applicability. And a good programmer should be aware of the distinction. The API exposed by the above mentioned Java classes blur this distinction. In my opinion, this is one of those areas where implementation hiding behind a common, easy-to-use interface (think of List interface that both ArrayList and LinkedList implement) may not be in the best interest of the primary user of these classes.


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产一区二区三区在线观看网站| 国产一区二区三区在线观看网站 | 国产一区二区三区观看| 亚洲私人黄色宅男| 一区二区高清视频在线观看| 欧美日韩一区在线播放| 亚洲免费网站| 亚洲欧美在线网| 国产综合香蕉五月婷在线| 另类图片综合电影| 欧美阿v一级看视频| 日韩视频免费在线| 99亚洲精品| 国色天香一区二区| 欧美成人免费视频| 欧美日韩国内自拍| 久久国产婷婷国产香蕉| 久久久久久香蕉网| 一区二区日韩伦理片| 亚洲夜间福利| 亚洲二区视频在线| 亚洲麻豆国产自偷在线| 国产情侣一区| 亚洲国产另类 国产精品国产免费| 久久亚洲精品欧美| 亚洲视频1区| 久久精品国产96久久久香蕉| 亚洲免费激情| 欧美一区二区在线免费播放| 亚洲国产另类精品专区 | 久久狠狠久久综合桃花| 欧美在线一二三四区| 亚洲精品乱码久久久久久蜜桃91| 亚洲图片在线观看| 亚洲黄色成人久久久| 亚洲性图久久| 亚洲精一区二区三区| 亚洲欧美激情一区| 亚洲美女黄色| 久久成人人人人精品欧| 亚洲香蕉网站| 欧美成人免费在线| 六月丁香综合| 国产欧美精品国产国产专区| 亚洲精品国产日韩| 伊人久久综合97精品| 亚洲色无码播放| 日韩小视频在线观看| 久久电影一区| 久久激情综合网| 国产精品盗摄久久久| 欧美国产三区| 亚洲成人在线观看视频| 香蕉精品999视频一区二区 | 精品9999| 亚洲线精品一区二区三区八戒| 亚洲人成小说网站色在线| 欧美综合国产| 欧美在线一二三| 国产精品成人播放| 9l国产精品久久久久麻豆| 亚洲国产欧美日韩精品| 久久久999成人| 久久久精彩视频| 国产精品入口日韩视频大尺度| 99视频+国产日韩欧美| 日韩亚洲视频| 欧美巨乳在线观看| 亚洲黄色影院| 日韩午夜在线观看视频| 欧美精品一区视频| 亚洲精选一区| 亚洲永久免费精品| 国产精品成人免费| 亚洲午夜激情免费视频| 欧美亚洲视频一区二区| 国产精品亚洲产品| 欧美亚洲三级| 久久人人爽国产| 在线日韩视频| 欧美黄色一区二区| 日韩一区二区精品葵司在线| 亚洲欧美日韩精品一区二区| 国产精品亚洲精品| 欧美一区久久| 亚洲福利视频一区| 正在播放亚洲一区| 国产欧美韩日| 久久综合导航| 亚洲精品在线视频观看| 亚洲欧美国产另类| 狠狠色综合色综合网络| 欧美好骚综合网| 亚洲一区二区精品在线| 久久久久久久久久久久久9999| 亚洲第一页自拍| 欧美日韩亚洲网| 欧美一区二区精品| 亚洲第一免费播放区| 亚洲在线一区| 极品裸体白嫩激情啪啪国产精品| 欧美v日韩v国产v| 亚洲午夜未删减在线观看| 鲁大师成人一区二区三区| 亚洲精品永久免费| 国产日韩精品综合网站| 欧美第一黄网免费网站| 亚洲主播在线播放| 欧美国内亚洲| 久久成人综合视频| 日韩午夜免费视频| 黄色在线一区| 国产精品久久久久久久久久免费看| 久久九九全国免费精品观看| 亚洲美女区一区| 美女视频网站黄色亚洲| 亚洲一区黄色| 亚洲精品一区二区三区樱花 | 国产精品第一页第二页第三页| 久久国产日韩欧美| 99综合在线| 亚洲国产精品ⅴa在线观看| 欧美在线播放高清精品| 一本久久a久久免费精品不卡| 国产亚洲一级高清| 国产精品亚洲人在线观看| 欧美激情精品久久久久久黑人| 欧美伊人影院| 午夜精品久久久久久久久久久久久 | 亚洲免费观看视频| 在线观看视频一区二区欧美日韩| 国产精品视频99| 欧美日韩一区二区免费在线观看| 猛男gaygay欧美视频| 久久久久久久久久久久久女国产乱| 亚洲一区二区三区四区在线观看 | 欧美在线1区| 亚洲淫性视频| 亚洲午夜电影在线观看| 一个色综合导航| 亚洲精品少妇| 亚洲巨乳在线| 一片黄亚洲嫩模| 亚洲视频免费| 亚洲中午字幕| 欧美一区二区三区精品电影| 亚洲欧美国产高清| 午夜精品在线| 午夜精品视频在线| 欧美一区二区精品在线| 欧美在线电影| 久久综合久久美利坚合众国| 久久女同精品一区二区| 欧美怡红院视频| 久久久九九九九| 老司机67194精品线观看| 久久一区国产| 麻豆久久婷婷| 欧美日韩另类丝袜其他| 国产精品国产成人国产三级| 国产精品h在线观看| 国产精品免费看| 韩国女主播一区| 亚洲激情影视| 亚洲一区免费网站| 欧美亚洲日本一区| 免费国产自线拍一欧美视频| 亚洲国产精品第一区二区| 日韩视频一区二区| 西瓜成人精品人成网站| 久久露脸国产精品| 欧美精品日韩一本| 国产精品久久久久天堂| 国产在线不卡精品| 亚洲欧洲在线一区| 亚洲影视在线| 男人的天堂亚洲| 亚洲最新在线| 久久精品亚洲热| 欧美日韩国产成人在线免费| 国产精品一区二区你懂的| 精品91在线| 亚洲专区一区| 欧美国产日韩一二三区| 99在线精品视频在线观看| 久久精品免费| 欧美午夜精品理论片a级按摩| 国产一区日韩一区| 一区二区三区久久| 久久中文欧美| 亚洲一区综合| 欧美精品v日韩精品v国产精品| 国产日韩欧美亚洲一区| 亚洲欧洲一区| 久久永久免费| 亚洲欧美日韩区| 欧美日韩免费看| 亚洲黄网站在线观看| 久久精品女人天堂| 一区二区三区久久|