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

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>
            国产精品亚发布| 国产热re99久久6国产精品| 影院欧美亚洲| 男女视频一区二区| 老司机67194精品线观看| …久久精品99久久香蕉国产| 欧美激情国产高清| 欧美精品在线观看一区二区| 亚洲女爱视频在线| 午夜久久久久| 影音先锋久久精品| 亚洲另类春色国产| 国产免费亚洲高清| 欧美波霸影院| 欧美亚日韩国产aⅴ精品中极品| 亚洲影院在线观看| 久久天堂国产精品| 亚洲视频自拍偷拍| 欧美在线二区| 99re6这里只有精品| 中文国产亚洲喷潮| 尤物在线观看一区| 99在线精品视频在线观看| 国产欧美婷婷中文| 亚洲激情在线| 国产视频精品xxxx| 日韩一级片网址| 激情成人av| 一区二区毛片| 亚洲国产成人高清精品| 亚洲一区二区在线看| 在线观看日韩精品| 亚洲无人区一区| 亚洲韩国青草视频| 午夜精品久久久久99热蜜桃导演| 亚洲人成绝费网站色www| 亚洲一区视频| 野花国产精品入口| 久久久久久亚洲精品中文字幕| 亚洲一区二区视频在线| 久久激情五月婷婷| 欧美一二三区在线观看| 欧美另类一区二区三区| 女人天堂亚洲aⅴ在线观看| 国产精品久久久久久久久久三级 | 欧美一区二区在线看| 夜夜嗨网站十八久久| 久久综合伊人77777| 欧美专区一区二区三区| 国产精品av免费在线观看| 亚洲第一福利社区| 18成人免费观看视频| 篠田优中文在线播放第一区| 亚洲欧美国产精品专区久久| 欧美日韩另类丝袜其他| 亚洲国产成人av在线| 在线看国产一区| 久久久91精品国产一区二区精品| 亚洲欧美一区二区三区在线 | 欧美有码视频| 欧美在线观看www| 国产精品女主播一区二区三区| 亚洲国产一区二区三区a毛片| 亚洲国产电影| 麻豆久久婷婷| 亚洲国产精品嫩草影院| 亚洲精品视频在线播放| 欧美丰满少妇xxxbbb| 亚洲欧洲一区二区天堂久久| 日韩视频免费观看高清在线视频| 欧美高清一区二区| 亚洲国产精品精华液2区45| 亚洲免费成人| 欧美日本国产一区| 宅男精品视频| 亚洲欧美中日韩| 国产一区自拍视频| 久久在线视频| 亚洲国产一区二区在线| 日韩视频免费| 国产精品视频观看| 久久精品99久久香蕉国产色戒| 久久在精品线影院精品国产| 激情成人中文字幕| 欧美成人免费全部| 一区二区三区四区五区在线| 欧美一区二区黄| 伊人夜夜躁av伊人久久| 美脚丝袜一区二区三区在线观看| 91久久久在线| 亚洲欧美日韩综合国产aⅴ| 国产精品无码专区在线观看| 久久九九热re6这里有精品| 欧美高清在线一区| 午夜电影亚洲| 亚洲片区在线| 国产久一道中文一区| 久久夜色精品国产| 亚洲午夜精品福利| 麻豆精品在线观看| 亚洲美女在线一区| 国内精品久久久久影院色| 欧美精品久久久久久久久老牛影院| 一本一本a久久| 欧美成人亚洲成人日韩成人| 亚洲图中文字幕| 亚洲国产日韩一区二区| 国产精品日韩精品欧美精品| 猫咪成人在线观看| 性做久久久久久免费观看欧美| 亚洲高清激情| 久久精彩免费视频| 亚洲素人在线| 91久久久在线| 精品成人一区二区| 国产精品毛片| 欧美日韩一区精品| 免费观看久久久4p| 久久精精品视频| 亚洲一区二区三| 亚洲国产精品嫩草影院| 老司机亚洲精品| 久久av一区二区三区漫画| 一区二区三区国产精华| 亚洲激情成人网| 激情久久久久久| 国产一区二区三区无遮挡| 国产精品福利av| 欧美另类99xxxxx| 欧美成人综合网站| 久久一区激情| 老司机aⅴ在线精品导航| 久久精品国产清高在天天线| 午夜精品久久久久久久久久久| av成人老司机| 日韩亚洲精品在线| 日韩视频在线观看国产| 亚洲乱码精品一二三四区日韩在线| 你懂的网址国产 欧美| 久久综合久久综合久久| 久久免费精品日本久久中文字幕| 久久av免费一区| 久久精品国产69国产精品亚洲| 久久av红桃一区二区小说| 欧美一区二区三区电影在线观看| 亚洲欧美在线x视频| 午夜精品免费| 久久国产精品久久久久久电车| 欧美一二三区精品| 久久精品成人一区二区三区蜜臀 | 欧美在线播放一区| 欧美一级午夜免费电影| 欧美在线观看视频一区二区三区| 欧美在线观看日本一区| 久久爱www| 久久影院午夜片一区| 欧美高清在线精品一区| 亚洲精品一级| 亚洲一区观看| 久久久最新网址| 欧美区二区三区| 国产精品色婷婷| 在线观看欧美亚洲| 一本一本久久| 久久www成人_看片免费不卡| 久久尤物视频| 日韩亚洲欧美一区二区三区| 亚洲一级片在线看| 久久精品国产视频| 欧美福利在线| 国产欧美精品在线观看| 在线观看一区视频| 一区二区三区成人精品| 久久久激情视频| 亚洲老司机av| 久久成人免费| 欧美体内谢she精2性欧美| 国内久久婷婷综合| 日韩午夜电影| 久久综合久久综合久久| 日韩亚洲欧美成人| 久久久欧美精品| 国产精品国产三级国产aⅴ浪潮| 国产亚洲精品bt天堂精选| 亚洲毛片播放| 可以看av的网站久久看| 日韩午夜免费| 久久视频一区二区| 国产精品一区二区久久久| 亚洲美女啪啪| 美女久久网站| 亚洲欧美影音先锋| 欧美日韩国产综合视频在线观看 | 99re热精品| 美女精品视频一区| 亚洲女优在线| 欧美日韩亚洲一区二区三区在线 | 亚洲人成网站777色婷婷| 欧美在线观看视频在线 | 久久精品99国产精品日本 |