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

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>
            亚洲精品日本| 亚洲三级免费观看| 亚洲一区二区久久| 欧美日韩免费区域视频在线观看| 亚洲国产一区二区a毛片| 欧美不卡在线视频| 你懂的成人av| 一本久道久久久| 夜夜夜久久久| 国产日韩一区二区三区在线播放 | 欧美电影免费观看高清| 最新成人在线| 日韩视频一区| 国产午夜精品理论片a级探花| 久久久久久久高潮| 麻豆精品网站| 午夜欧美不卡精品aaaaa| 欧美一区二区精品| 亚洲国产精品电影| 亚洲视频久久| 亚洲第一视频| 亚洲日本免费电影| 国产精品草草| 乱中年女人伦av一区二区| 蜜桃av一区二区| 亚洲综合电影一区二区三区| 欧美亚洲一区| 亚洲毛片av| 欧美在线三级| 夜色激情一区二区| 久久福利精品| 亚洲网站啪啪| 久久夜色精品国产| 亚洲欧美中文字幕| 蜜桃久久av一区| 久久九九免费视频| 欧美日韩免费高清| 免费成人黄色| 国产美女精品在线| 一本久久a久久精品亚洲| 在线播放中文一区| 欧美亚洲免费电影| 一本一本久久| 久久综合伊人| 久久久久久久久久久久久9999| 欧美日本三区| 欧美激情第8页| 精品99一区二区| 亚洲影院免费| 在线亚洲国产精品网站| 欧美α欧美αv大片| 久久婷婷影院| 国产一区二区精品久久99| 99视频精品全部免费在线| 亚洲日本免费电影| 蜜桃av一区| 美日韩精品视频免费看| 国产亚洲欧美日韩精品| 亚洲手机成人高清视频| 在线亚洲免费| 欧美日韩一区二区高清| 亚洲精品乱码久久久久久黑人| 尤物yw午夜国产精品视频| 亚洲欧美日韩一区在线| 亚洲女同精品视频| 国产精品免费aⅴ片在线观看| 日韩亚洲欧美精品| 这里只有精品视频在线| 欧美日韩国产三区| 亚洲精品社区| 亚洲一二三四久久| 欧美视频中文一区二区三区在线观看 | 国产日韩精品一区二区三区在线| 亚洲黄网站在线观看| 亚洲精品老司机| 欧美激情久久久久| 亚洲欧洲视频| 亚洲四色影视在线观看| 国产精品久久久久久久久久久久久| 一本大道久久a久久精品综合| 亚洲色图在线视频| 国产精品国产三级国产专播品爱网| aaa亚洲精品一二三区| 亚洲欧美日韩区| 国产日韩亚洲| 免费观看成人www动漫视频| 亚洲第一福利视频| 日韩亚洲视频| 国产精品视频一| 亚洲在线视频免费观看| 久久久久综合一区二区三区| 在线精品国产欧美| 欧美日韩国产丝袜另类| 亚洲性视频网站| 麻豆乱码国产一区二区三区| 亚洲日本欧美天堂| 国产精品丝袜久久久久久app| 欧美一区二区三区视频免费播放| 欧美mv日韩mv亚洲| 一二三区精品福利视频| 国产女主播一区二区三区| 久久精品一区中文字幕| 日韩一区二区福利| 久久噜噜亚洲综合| 中文在线资源观看网站视频免费不卡| 国产嫩草一区二区三区在线观看 | 亚洲精品中文字幕有码专区| 午夜精品久久久久久久99水蜜桃| 国产一区二区三区黄视频| 欧美成人高清| 亚洲嫩草精品久久| 欧美大片免费| 欧美自拍丝袜亚洲| 亚洲视频在线二区| 亚洲第一精品电影| 国产欧美一区二区精品仙草咪 | 久久九九国产精品| 亚洲精品之草原avav久久| 久久免费黄色| 亚洲欧美激情四射在线日 | 亚洲一区二区三区精品在线观看| 国产综合网站| 欧美午夜精品一区二区三区| 久久久久久综合网天天| 亚洲制服少妇| 亚洲美女中文字幕| 欧美大片免费观看| 久久久久网址| 欧美在线高清| 亚洲欧美日韩在线| 亚洲午夜高清视频| 亚洲免费精彩视频| 91久久在线播放| 亚洲第一精品久久忘忧草社区| 国产亚洲精品美女| 国产精品乱子久久久久| 欧美日韩在线观看一区二区| 美女精品在线| 麻豆成人在线播放| 久久久欧美精品| 久久久久久久久久看片| 欧美一区二区三区在线观看视频| 亚洲网站在线| 亚洲在线中文字幕| 亚洲欧美制服另类日韩| 亚洲在线一区二区三区| 亚洲在线视频网站| 午夜精品视频在线| 欧美在线3区| 欧美在线播放| 久久看片网站| 另类专区欧美制服同性| 久热re这里精品视频在线6| 久久久亚洲影院你懂的| 久久香蕉国产线看观看av| 久久久噜噜噜久久| 免费欧美日韩| 欧美另类69精品久久久久9999| 欧美精品二区| 欧美午夜电影一区| 国产精品日韩专区| 国产一区导航| 亚洲国产精品传媒在线观看 | 亚洲精品日日夜夜| 亚洲天堂成人| 欧美在线资源| 美女成人午夜| 91久久久在线| 亚洲伊人伊色伊影伊综合网| 羞羞视频在线观看欧美| 久久亚洲春色中文字幕久久久| 男女激情久久| 国产精品久久久久久亚洲调教| 国产欧美日韩一区二区三区在线观看 | 亚洲国产一区二区a毛片| 亚洲精品五月天| 欧美一级淫片播放口| 免费观看成人鲁鲁鲁鲁鲁视频| 亚洲精品1区2区| 亚洲欧美激情视频在线观看一区二区三区 | 老牛影视一区二区三区| 亚洲激情小视频| 香蕉久久夜色精品国产| 欧美α欧美αv大片| 国产伦精品一区二区三区四区免费 | av成人免费| 久久久久久黄| 99视频精品在线| 久久久蜜桃精品| 国产精品久久久久免费a∨大胸| 狠狠色丁香婷婷综合| 这里只有精品视频| 欧美jjzz| 欧美一区二区三区视频免费| 欧美精品www| 亚洲第一狼人社区| 欧美中文字幕| 一片黄亚洲嫩模| 欧美成人精品h版在线观看| 国产亚洲欧美一区|