亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

正確使用ArrayList和LinkedList—性能的改進

2019-11-18 13:26:06
字體:
來源:轉載
供稿:網友

  USING ARRAYLIST AND LINKEDLIST
  
  ArrayList and LinkedList are two Collections classes used for storing lists of object references. For example, you could have an ArrayList of Strings, or a LinkedList of Integers. This tip compares the performance of ArrayList and LinkedList, and offers some suggestions about which of these classes is the right choice in a given situation.
  
  The first key point is that an ArrayList is backed by a PRimitive Object array. Because of that, an ArrayList is mUCh faster than a LinkedList for random access, that is, when accessing arbitrary list elements using the get method. Note that the get method is implemented for LinkedLists, but it requires a sequential scan from the front or back of the list. This scan is very slow. For a LinkedList, there's no fast way to access the Nth element of the list.
  
  Consider the following example. Suppose you have a large list of sorted elements, either an ArrayList or a LinkedList. Suppose too that you do a binary search on the list. The standard binary search algorithm starts by checking the search key against the value in the middle of the list. If the middle value is too high, then the upper half of the list is eliminated. However, if the middle value is too low, then the lower half of the list is ignored. This process continues until the key is found in the list, or until the lower bound of the search becomes greater than the upper bound.
  
  Here's a program that does a binary search on all the elements in an ArrayList or a LinkedList:
  
  import java.util.*;
  
  public class ListDemo1 {
  static final int N = 10000;
  
  static List values;
  
  // make List of increasing Integer values
  
  static {
  Integer vals[] = new Integer[N];
  
  Random rn = new Random();
  
  for (int i = 0, currval = 0; i < N; i++) {
  vals[i] = new Integer(currval);
  currval += rn.nextInt(100) + 1;
  }
  
  values = Arrays.asList(vals);
  }
  
  // iterate across a list and look up every
  // value in the list using binary search
  
  static long timeList(List lst) {
  long start = System.currentTimeMillis();
  
  for (int i = 0; i < N; i++) {
  
  // look up a value in the list
  // using binary search
  
  int indx = Collections.binarySearch(
  lst, values.get(i));
  
  // sanity check for result
  // of binary search
  
  if (indx != i) {
  System.out.println(
  "*** error ***/n");
  }
  }
  
  return System.currentTimeMillis() - start;
  }
  
  public static void main(String args[]) {
  
  // do lookups in an ArrayList
  
  System.out.println("time for ArrayList = " +
  timeList(new ArrayList(values)));
  
  // do lookups in a LinkedList
  
  System.out.println(
  "time for LinkedList = " +
  timeList(new LinkedList(values)));
  }
  }
  
  The ListDemo1 program sets up a List of sorted Integer values. It then adds the values to an ArrayList or a LinkedList. Then Collections.binarySearch is used to search for each value in the list.
  
  When you run this program, you should see a result that looks something like this:
  
  time for ArrayList = 31
  
  time for LinkedList = 4640
  
  ArrayList is about 150 times faster than LinkedList. (Your results might differ depending on your machine characteristics, but you should see a distinct difference in the result for ArrayList as compared to that for LinkedList. The same is true for the other programs in this tip.) Clearly, LinkedList is a bad choice in this situation. The binary search algorithm inherently uses random access, and LinkedList does not support fast random access. The time to do a random access in a LinkedList is proportional to the size of the list. By comparison, random access in an ArrayList has a fixed time.
  
  You can use the RandomAccess marker interface to check whether a List supports fast random access:
  
  void f(List lst) {
  if (lst instanceof RandomAccess) {
  // supports fast random access
  }
  }
  
  ArrayList implements the RandomAccess interface, and LinkedList. does not. Note that Collections.binarySearch does take advantage of the RandomAccess property, to optimize searches.
  
  Do these results prove that ArrayList is always a better choice? Not necessarily. There are many cases where LinkedList does better. Also note that there are many situations where an algorithm can be implemented efficiently for LinkedList. An example is reversing a LinkedList using Collections.reverse. The internal algorithm does this, and gets reasonable performance, by using forward and backward iterators.
  
  Let's look at another example. Suppose you have a list of elements, and you do a lot of element inserting and deleting to the list. In this case, LinkedList is the better choice. To demonstrate that, consider the following "worst case" scenario. In this demo, a program repeatedly inserts elements at the beginning of a list. The code looks like this:
  
  import java.util.*;
  
  public class ListDemo2 {
  static final int N = 50000;
  
  // time how long it takes to add
  // N objects to a list
  
  static long timeList(List lst) {
  long start = System.currentTimeMillis();
  
  Object obj = new Object();
  
  for (int i = 0; i < N; i++) {
  lst.add(0, obj);
  }
  
  return System.currentTimeMillis() - start;
  }
  
  public static void main(String args[]) {
  
  // do timing for ArrayList
  
  System.out.println(
  "time for ArrayList = " +
  timeList(new ArrayList()));
  
  // do timing for LinkedList
  
  System.out.println(
  "time for LinkedList = " +
  timeList(new LinkedList()));
  }
  }
  
  When you run this program, the result should look something like this:
  
  time for ArrayList = 4859
  
  time for LinkedList = 125
  
  These results are pretty much the reverse of the previous example.
  
  When an element is added to the beginning of an ArrayList, all of the existing elements must be pushed back, which means a lot of eXPensive data movement and copying. By contrast, adding an element to the beginning of a LinkedList simply means allocating an internal record for the element and then adjusting a couple of links. Adding to the beginning of a LinkedList has fixed cost, but adding to the beginning of an ArrayList has a cost that's proportional to the list size.
  
  So far, this tip has looked at speed issues, but what about space? Let's look at some internal details of how ArrayList and LinkedList are implemented in Java 2 SDK, Standard Edition v 1.4. These details are not part of the external specification of these classes, but are illustrative of how such classes work internally.
  
  The LinkedList class has a private internal class defined like this:
  
  private static class Entry {
  Object element;
  Entry next;
  Entry previous;
  }
  
  Each Entry object references a list element, along with the next and previous elements in the LinkedList -- in other Words, a doubly-linked list. A LinkedList of 1000 elements will have 1000 Entry objects linked together, referencing the actual list elements. There is significant space overhead in a LinkedList structure, given all these Entry objects.
  
  An ArrayList has a backing Object array to sto

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美亚洲另类激情另类| 韩国视频理论视频久久| 国产精品尤物福利片在线观看| 亚洲精品第一国产综合精品| 欧美激情综合色综合啪啪五月| 色偷偷偷综合中文字幕;dd| 美女啪啪无遮挡免费久久网站| 亚洲国产精品成人精品| 国产激情久久久久| 欧美视频裸体精品| 伦伦影院午夜日韩欧美限制| 日本精品中文字幕| 日韩一区二区三区国产| 精品中文字幕在线| 97视频免费在线看| 欧美做受高潮电影o| 亚洲性av在线| 久久精品国产99国产精品澳门| 久久精品一偷一偷国产| xvideos亚洲人网站| 亚洲最大的成人网| 一本色道久久88亚洲综合88| 日韩av不卡电影| 国产美女精品视频免费观看| 91视频国产精品| 国产精品69久久久久| 欧美日韩激情视频8区| 欧美精品国产精品日韩精品| 色与欲影视天天看综合网| 91精品在线影院| 国产一区二区美女视频| 欧美亚洲国产视频小说| 综合网日日天干夜夜久久| 久久色免费在线视频| 播播国产欧美激情| 日韩免费精品视频| 亚洲欧美中文日韩v在线观看| 国产亚洲视频在线观看| 中文字幕成人在线| 亚洲第一av网站| 综合网中文字幕| 亚洲精品乱码久久久久久金桔影视| 高清欧美性猛交xxxx黑人猛交| 91亚洲国产精品| 欧美在线视频免费观看| 欧美一二三视频| 97碰碰碰免费色视频| 久久久久国产精品www| 精品国产一区二区三区久久久狼| 国内精品久久久久久久| 亚洲乱码一区av黑人高潮| 欧美在线不卡区| 亚洲社区在线观看| 亚洲一区二区久久久久久久| 亚洲美女精品久久| 日韩欧美中文字幕在线播放| 久久五月天色综合| 亚洲国产精品久久久| 久久中文字幕国产| 日韩成人av一区| 国产不卡av在线| 欧美另类老女人| 美女性感视频久久久| 亚洲精品日韩在线| 欧美亚洲一区在线| 国产欧美一区二区三区久久人妖| 国产精品视频播放| 97国产一区二区精品久久呦| 亚洲一区二区日本| 欧美香蕉大胸在线视频观看| 国产精品免费一区二区三区都可以| 亚洲一区二区免费| 精品福利一区二区| 国产激情视频一区| 91欧美日韩一区| 亚洲成av人乱码色午夜| 欧美日韩亚洲视频一区| 午夜精品99久久免费| 国产最新精品视频| 亚洲欧美日韩一区在线| 国产成人久久久| 国产精彩精品视频| 国产99久久精品一区二区 夜夜躁日日躁| 国产精品久久久久久亚洲影视| 91免费观看网站| 亚洲一区国产精品| 国产亚洲欧洲高清一区| 这里只有精品在线播放| 国产精品 欧美在线| 国产亚洲一区二区在线| 日韩精品福利在线| 欧美精品在线网站| 欧美亚洲日本黄色| 亚洲午夜久久久久久久| 午夜精品一区二区三区在线视| 欧美电影免费播放| 国产日韩在线看| 亚洲欧美制服中文字幕| 久久综合亚洲社区| 亚洲小视频在线观看| 狠狠色狠色综合曰曰| 久久在线精品视频| 亚洲精品一区在线观看香蕉| 成人乱人伦精品视频在线观看| 国产精品欧美激情在线播放| 欧美另类99xxxxx| 日韩在线中文字| 97精品久久久| 国产在线精品成人一区二区三区| 亚洲成年人在线| 欧美激情图片区| 色先锋资源久久综合5566| 中文字幕亚洲二区| 91精品国产91久久久久福利| 欧美一级在线亚洲天堂| 欧美中文字幕在线视频| 亚洲精品一区二三区不卡| 亚洲国产精品va在线看黑人| 一区二区中文字幕| 国产欧美一区二区三区在线| 8050国产精品久久久久久| 亚洲国产91精品在线观看| 久久精品视频导航| 久久九九免费视频| 亚洲一区二区中文字幕| 亚洲欧美日韩区| 亚洲国产精品人人爽夜夜爽| 亚洲欧美制服中文字幕| 欧美日韩亚洲一区二| 在线播放精品一区二区三区| 欧美片一区二区三区| 日韩中文字幕免费视频| 久久夜色精品国产| 日韩中文字幕国产精品| 日本久久久a级免费| 国产视频福利一区| 久久久久女教师免费一区| 久久久久久av| 美女久久久久久久久久久| 国产精品∨欧美精品v日韩精品| 国产成人精品久久久| 久久久久久伊人| 欧美一级视频免费在线观看| 97视频在线观看免费高清完整版在线观看| 91久久精品国产91性色| 亚洲aaaaaa| 成人免费xxxxx在线观看| www.99久久热国产日韩欧美.com| 成人免费视频网址| 91精品综合久久久久久五月天| 国外成人性视频| 中文字幕亚洲无线码在线一区| 亚洲精品网站在线播放gif| 精品久久久免费| 国产精品欧美日韩一区二区| 成人综合网网址| 亚洲精品有码在线| 国产精品久久久久99| 欧美一级大片在线观看| 91精品国产色综合| 日本19禁啪啪免费观看www| 国产欧美最新羞羞视频在线观看| 92看片淫黄大片看国产片| 国产日韩在线观看av|