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

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>
            免费欧美在线| 久久五月婷婷丁香社区| 欧美视频日韩视频在线观看| 亚洲靠逼com| 夜夜爽www精品| 国产女主播一区二区三区| 久久精品欧美日韩| 久久午夜色播影院免费高清| 亚洲肉体裸体xxxx137| 亚洲毛片在线看| 国产精品色午夜在线观看| 久久夜色精品国产噜噜av| 免费成人网www| 亚洲一区二区三区在线播放| 欧美一区二区私人影院日本| 亚洲欧洲日本一区二区三区| 一本大道久久精品懂色aⅴ| 国产婷婷精品| 亚洲区中文字幕| 国产精品毛片a∨一区二区三区| 久久精品女人| 欧美精品在线观看播放| 久久精品国产清自在天天线| 欧美另类高清视频在线| 久久精品二区| 久久久久国产精品一区三寸| 欧美黑人在线观看| 久久精品国产一区二区三区免费看| 免费不卡在线观看av| 久久国产精品黑丝| 欧美日韩精品免费观看视频| 免费视频一区| 国产伦精品一区二区三区视频黑人| 亚洲国产高清一区| 韩国一区二区三区美女美女秀| 亚洲精品在线三区| 亚洲国产精品一区二区第四页av| 亚洲欧美综合| 亚洲免费影视第一页| 欧美人与性禽动交情品 | 亚洲精品国产视频| 欧美在线视频导航| 午夜国产精品影院在线观看| 欧美成人综合在线| 欧美成人高清视频| 国产在线精品成人一区二区三区| 国产精品99久久久久久白浆小说 | 欧美日韩亚洲网| 亚洲国产一区在线| 亚洲成色精品| 久久久久综合网| 久热精品视频在线免费观看| 国产区二精品视| 亚洲欧美日韩另类| 亚洲欧美日韩中文视频| 欧美性jizz18性欧美| 日韩视频在线播放| aa级大片欧美| 欧美日韩国产bt| 亚洲精品一区二区三区樱花| 日韩午夜精品视频| 欧美看片网站| 夜色激情一区二区| 亚洲欧美精品在线| 国产欧美一区二区三区沐欲| 亚洲欧美日韩视频一区| 欧美一区二区在线看| 国产自产2019最新不卡| 久久久久成人精品免费播放动漫| 久久一区欧美| 亚洲激情在线观看| 欧美日本国产| 亚洲愉拍自拍另类高清精品| 久久精品国产91精品亚洲| 国产在线播精品第三| 蜜臀av性久久久久蜜臀aⅴ四虎| 免费人成精品欧美精品| 亚洲精品一区二区在线| 欧美日韩国产丝袜另类| 亚洲视频在线一区| 久久国产视频网站| 亚洲国产婷婷综合在线精品| 欧美精品国产一区二区| 亚洲午夜国产成人av电影男同| 欧美在线观看视频在线| 一区二区在线视频观看| 欧美v国产在线一区二区三区| 亚洲另类春色国产| 欧美专区亚洲专区| 亚洲三级观看| 国产精品亚洲综合天堂夜夜| 久久午夜电影| 亚洲天堂偷拍| 国语精品中文字幕| 欧美va天堂| 亚洲免费网址| 亚洲高清视频在线| 欧美一区二区三区四区视频| 91久久夜色精品国产九色| 国产精品美女久久| 蜜臀a∨国产成人精品| 亚洲在线视频| 亚洲欧洲精品一区二区三区| 久久久久国产一区二区三区四区| 亚洲三级国产| 精品动漫3d一区二区三区| 欧美三日本三级三级在线播放| 久久精品国产免费| 亚洲一区二区在线视频| 亚洲国产综合视频在线观看| 久久精品视频免费播放| 一区二区三区精品视频在线观看| 国产综合一区二区| 国产精品欧美一区二区三区奶水| 男女激情视频一区| 久久久美女艺术照精彩视频福利播放| 亚洲人成人一区二区三区| 久久综合久久综合久久综合| 亚洲欧美日韩天堂一区二区| 99国产精品视频免费观看| 亚洲第一网站免费视频| 国产一区二区三区在线观看网站 | 国产乱肥老妇国产一区二 | 欧美成人精精品一区二区频| 午夜精品久久久久| 亚洲视频免费看| 亚洲靠逼com| 亚洲人成人一区二区在线观看| 男同欧美伦乱| 久久露脸国产精品| 久久天堂国产精品| 久久久www免费人成黑人精品 | 亚洲午夜精品一区二区三区他趣| 亚洲国产一区二区三区高清| 狠狠爱www人成狠狠爱综合网| 国产精品永久免费观看| 国产精品一区二区在线观看网站 | 亚洲欧洲精品一区二区| 悠悠资源网亚洲青| 在线观看视频一区| 亚洲国产mv| 亚洲区中文字幕| 亚洲日本aⅴ片在线观看香蕉| 精品动漫3d一区二区三区免费版| 国产最新精品精品你懂的| 国产三级精品在线不卡| 国产亚洲亚洲| 怡红院精品视频| 亚洲国产精品久久| 99av国产精品欲麻豆| 亚洲一区二区三区欧美| 亚洲欧美日韩国产综合| 欧美资源在线| 欧美91视频| 亚洲精品乱码久久久久久日本蜜臀 | 欧美成人精品| 亚洲欧洲在线看| 亚洲午夜精品国产| 欧美亚洲专区| 欧美1区免费| 国产精品av一区二区| 国产欧美精品一区aⅴ影院| 一区二区在线看| 一区二区三区精密机械公司 | 久久精品女人| 亚洲高清在线播放| 亚洲一区国产| 麻豆国产精品va在线观看不卡| 欧美精品入口| 国产精品私人影院| 亚洲国产精品久久人人爱蜜臀| 99视频超级精品| 久久精品国产在热久久| 亚洲高清中文字幕| 亚洲尤物精选| 欧美国产综合视频| 国产欧美在线播放| 亚洲毛片在线| 久久免费视频在线| 亚洲精品日韩久久| 欧美在线一二三| 欧美日韩一区二区在线视频| 国产亚洲欧美日韩日本| 国产精品99久久久久久www| 久久免费视频观看| 在线亚洲高清视频| 女主播福利一区| 国产视频亚洲精品| 中国av一区| 欧美国产精品中文字幕| 午夜精品福利视频| 欧美视频一区二区三区在线观看| 在线播放亚洲| 欧美在线视屏| 亚洲视频电影在线| 欧美日韩国产成人精品| 亚洲国产专区| 美女精品视频一区| 欧美一区二区三区日韩| 国产精品久久久一区二区三区 |