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

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.


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   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>
            99视频精品在线| 亚洲国产精品毛片| 亚洲性色视频| 国产欧美91| 久久久久久自在自线| 久久久视频精品| 日韩视频三区| 亚洲一二三区在线观看| 激情六月婷婷综合| 亚洲黄一区二区| 国产精品视频免费一区| 久久视频在线视频| 欧美激情一区二区三区成人| 午夜视频一区在线观看| 久久精品国产2020观看福利| 日韩一区二区精品视频| 亚洲欧美日韩第一区 | 亚洲最新色图| 国产一区二区三区黄| 欧美激情一区二区三区在线视频| 欧美日韩精品免费看 | 亚洲图片欧美午夜| 久久久久九九九| 亚洲一区日韩在线| 久久久久一区二区| 亚洲女优在线| 免费久久99精品国产| 午夜激情综合网| 欧美国产成人精品| 久久久久久久久岛国免费| 欧美日韩亚洲一区二区三区在线| 久久一区中文字幕| 国产农村妇女毛片精品久久麻豆| 亚洲国产一成人久久精品| 国产精品一区二区a| 亚洲精品久久久久久久久久久久| 国产一区二区成人| 一区二区三区视频免费在线观看| 亚洲欧洲日本在线| 久久久www成人免费无遮挡大片 | 久久综合九九| 国产精品毛片a∨一区二区三区| 欧美福利视频网站| 黄色成人在线网站| 欧美一区二区三区精品电影| 亚洲男人第一av网站| 最近中文字幕日韩精品| 久久精品综合网| 久久免费精品视频| 国产视频亚洲精品| 亚洲欧美在线一区| 久久成人人人人精品欧| 国产精品每日更新在线播放网址| 亚洲三级观看| 99在线|亚洲一区二区| 欧美成年人网站| 亚洲福利视频一区| 亚洲国产裸拍裸体视频在线观看乱了| 久久se精品一区二区| 久久久精品一品道一区| 国产日韩欧美麻豆| 久久精品国产免费观看| 久久午夜精品| 在线观看亚洲精品| 欧美大胆成人| 日韩网站免费观看| 亚洲欧美制服另类日韩| 国产欧美大片| 久久久久久噜噜噜久久久精品| 牛人盗摄一区二区三区视频| 亚洲激情视频在线播放| 欧美精品在线观看播放| 洋洋av久久久久久久一区| 亚洲一区二区欧美| 国产亚洲精品福利| 免费在线欧美黄色| 99精品黄色片免费大全| 小辣椒精品导航| 国内精品久久久| 免费观看一级特黄欧美大片| 亚洲日本欧美| 欧美一区在线视频| 亚洲国产精品成人综合| 欧美日韩国产系列| 亚洲欧美日韩中文播放| 欧美成人官网二区| 亚洲网站视频| 国产亚洲毛片| 欧美激情在线| 午夜精品久久久久久久99黑人| 男人的天堂亚洲| 亚洲小说春色综合另类电影| 国产亚洲欧洲一区高清在线观看| 久久这里只有精品视频首页| 99视频精品| 蜜臀久久99精品久久久久久9 | 国产精品一区二区三区久久久| 久久精品在线免费观看| 亚洲精品中文字幕女同| 久久久一区二区| 亚洲尤物视频在线| 亚洲高清在线观看| 国产精品久久激情| 欧美激情视频网站| 欧美主播一区二区三区美女 久久精品人 | 久久成人免费网| 一区二区三区精品视频在线观看| 国产一区二区三区在线观看免费| 欧美激情在线狂野欧美精品| 欧美在线视频导航| 亚洲神马久久| 亚洲国内精品在线| 欧美成人高清| 久久躁狠狠躁夜夜爽| 亚洲欧美成人一区二区在线电影| 亚洲七七久久综合桃花剧情介绍| 国产一区二区剧情av在线| 欧美日韩亚洲激情| 欧美精品xxxxbbbb| 猛男gaygay欧美视频| 久久精品一区二区三区不卡牛牛| 亚洲桃色在线一区| 日韩视频亚洲视频| 亚洲国产视频直播| 欧美高清不卡在线| 免费久久精品视频| 久久一区二区三区四区| 久久精品国产一区二区三| 亚洲在线第一页| 亚洲免费在线视频一区 二区| 一本色道久久加勒比精品| 亚洲电影免费观看高清| 国产日韩欧美在线视频观看| 国产精品人人爽人人做我的可爱| 欧美色图麻豆| 欧美日韩伦理在线| 欧美日本高清视频| 欧美激情在线免费观看| 欧美第一黄色网| 欧美激情一区在线| 欧美激情中文字幕一区二区| 久久婷婷国产综合精品青草| 久久久www成人免费精品| 久久婷婷国产综合国色天香| 美女露胸一区二区三区| 久久综合久久美利坚合众国| 久久久久久国产精品一区| 久久九九国产| 猛干欧美女孩| 欧美日韩成人激情| 国产精品啊啊啊| 国产日韩欧美不卡| 精品福利免费观看| 亚洲精品一级| 亚洲在线一区二区| 欧美日韩精品免费看 | 欧美国内亚洲| 欧美日韩中字| 国产偷自视频区视频一区二区 | 欧美伦理在线观看| 欧美日韩亚洲天堂| 国产亚洲aⅴaaaaaa毛片| 伊人婷婷欧美激情| 一个人看的www久久| 欧美一区二区三区视频在线观看 | 亚洲欧美日韩中文视频| 欧美在线www| 欧美成人资源网| 一区二区三区精品视频| 欧美一区免费视频| 欧美精品在线观看一区二区| 国产欧亚日韩视频| 亚洲精品国产系列| 欧美一区日本一区韩国一区| 欧美电影电视剧在线观看| 亚洲免费成人| 久久免费国产精品| 国产精品欧美一区二区三区奶水| 精品51国产黑色丝袜高跟鞋| 99亚洲伊人久久精品影院红桃| 久久久精品国产免大香伊| 亚洲三级电影在线观看| 久久精品成人一区二区三区 | 欧美在线你懂的| 欧美色网一区二区| 亚洲电影免费观看高清完整版在线观看| 在线亚洲伦理| 欧美国产日韩亚洲一区| 亚洲在线电影| 欧美日韩一区二区在线| 在线精品国精品国产尤物884a| 亚洲免费综合| 亚洲美女淫视频| 麻豆亚洲精品| 国内精品免费午夜毛片| 亚洲专区免费| 99精品视频免费观看视频| 欧美大片免费久久精品三p| 激情欧美一区| 久久国产精品久久久久久久久久|