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

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.

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲精品偷拍| 亚洲精品一区二区三区在线观看| 影音先锋久久| 久久人人爽爽爽人久久久| 亚洲网站在线看| 亚洲伊人伊色伊影伊综合网| 亚洲欧美成人网| 欧美福利专区| 亚洲国产成人av在线| 小嫩嫩精品导航| 久久九九国产| 欧美成人xxx| 国产精品成av人在线视午夜片| 免费在线亚洲欧美| 亚洲黄色影片| 狠狠干成人综合网| 国内激情久久| 亚洲国产精品v| 免费观看亚洲视频大全| 欧美成人免费播放| 亚洲欧美日韩国产综合精品二区| 国产区欧美区日韩区| 国产一区二区无遮挡| 国产亚洲欧美日韩一区二区| 欧美在线视频不卡| 久久久久久日产精品| 亚洲大片av| 欧美成人首页| 欧美精品网站| 久久岛国电影| 欧美h视频在线| 亚洲主播在线观看| 欧美在线你懂的| 蜜桃精品一区二区三区| 99在线精品视频在线观看| 亚洲午夜黄色| 亚洲高清免费在线| 一区二区三区欧美视频| 国产主播一区| aⅴ色国产欧美| 午夜在线精品偷拍| 亚洲国产女人aaa毛片在线| 艳女tv在线观看国产一区| 国产亚洲欧洲| 日韩一级免费| 国产欧美在线看| 亚洲美女精品一区| 国产一区二区三区在线观看网站| 亚洲剧情一区二区| 亚洲第一免费播放区| 亚洲免费av片| 亚洲国产精品va在线看黑人 | 国产亚洲毛片在线| 亚洲精品裸体| 在线看不卡av| 午夜视频一区二区| 亚洲性视频h| 欧美制服丝袜第一页| 欧美高清视频www夜色资源网| 午夜天堂精品久久久久| 欧美精品久久久久久久| 欧美成人免费全部观看天天性色| 国产精品中文字幕欧美| 亚洲精品少妇30p| 国产日韩成人精品| 一区二区精品国产| 日韩小视频在线观看| 亚洲综合色婷婷| 在线观看一区| 久久精品论坛| 久久精品91久久久久久再现| 欧美午夜不卡| 亚洲视频久久| 亚洲专区在线视频| 国产精品久久国产三级国电话系列| 亚洲国产成人午夜在线一区| 亚洲高清色综合| 久久亚洲私人国产精品va| 久久综合亚州| 亚洲国产成人不卡| 久久久久久噜噜噜久久久精品| 午夜精品久久久久久久蜜桃app| 久久夜色精品国产| 欧美激情精品久久久久久免费印度| 激情视频亚洲| 久久婷婷蜜乳一本欲蜜臀| 国产欧美短视频| 亚洲小说春色综合另类电影| 西西人体一区二区| 国产亚洲欧美日韩日本| 久久女同互慰一区二区三区| 欧美高清不卡| 在线综合+亚洲+欧美中文字幕| 欧美日韩你懂的| 欧美中文字幕在线视频| 国产日韩欧美在线观看| 久久精品国产一区二区三区| 免费观看日韩av| 91久久国产自产拍夜夜嗨| 欧美日韩国产美| 亚洲一区视频在线| 国产欧美日韩三区| 久久久久久久精| 亚洲国产一区二区在线| 日韩视频在线一区二区| 国产精品久久久久国产a级| 欧美一区二区三区视频免费播放| 老牛国产精品一区的观看方式| 亚洲国产一区二区精品专区| 欧美精品一区二区在线观看| 亚洲女性裸体视频| 欧美成人中文| 香蕉成人伊视频在线观看| 精品1区2区3区4区| 欧美日韩国产二区| 欧美亚洲一区二区在线观看| 欧美激情精品久久久久久免费印度| 这里只有精品电影| 激情婷婷亚洲| 欧美日韩亚洲一区二区三区四区| 午夜精品久久久久久久久久久久久| 另类av导航| 亚洲伊人伊色伊影伊综合网| 亚洲国产成人91精品| 国产精品视频一二三| 欧美大片免费| 亚洲欧美资源在线| 99精品免费视频| 在线成人欧美| 国产精品久久亚洲7777| 欧美不卡一卡二卡免费版| 性欧美8khd高清极品| 日韩一级大片| 亚洲国产一区二区视频| 久久婷婷国产综合精品青草| 亚洲一区二区三区在线视频| 亚洲国产人成综合网站| 国产一区二区三区黄| 国产精品xxx在线观看www| 麻豆成人在线播放| 久久狠狠婷婷| 篠田优中文在线播放第一区| 99热在这里有精品免费| 亚洲品质自拍| 欧美华人在线视频| 免费黄网站欧美| 久久人人97超碰国产公开结果| 欧美亚洲视频在线观看| 亚洲欧洲av一区二区| 日韩午夜av电影| 亚洲巨乳在线| 亚洲人成高清| 亚洲国产老妈| 91久久精品日日躁夜夜躁欧美| 国内视频一区| 尤妮丝一区二区裸体视频| 国产日韩一区在线| 国产一区二区黄色| 国产婷婷色一区二区三区四区| 国产精品日韩在线| 国产精品日韩欧美综合| 国产精品日韩欧美一区| 国产精品福利网站| 国产精品男人爽免费视频1| 国产精品乱码一区二三区小蝌蚪| 国产精品久久国产三级国电话系列| 欧美三级韩国三级日本三斤| 欧美日韩一区二区高清| 国产精品看片资源| 国产日韩视频| 永久免费毛片在线播放不卡| 亚洲第一精品久久忘忧草社区| 亚洲区一区二| 亚洲影院在线观看| 欧美专区第一页| 久久免费高清视频| 欧美国产日本高清在线| 最近看过的日韩成人| 一区二区三区鲁丝不卡| 欧美在线亚洲一区| 乱人伦精品视频在线观看| 久久一日本道色综合久久| 久久婷婷国产综合国色天香| 久久精品国产亚洲aⅴ| 久久精品久久综合| 久久精品亚洲一区| 欧美一区二区在线播放| 久热这里只精品99re8久| 欧美激情一区二区三区蜜桃视频| 欧美大片在线看免费观看| 欧美 日韩 国产 一区| 欧美精品二区三区四区免费看视频| 欧美成人免费观看| 国产精品第一区| 国产精品久久毛片a| 亚洲高清在线精品| 午夜视黄欧洲亚洲| 亚洲欧美日韩精品| 欧美www在线| 国产精品色午夜在线观看|