Dowemo
0 0 0 0

ArrayList 数组列表
LinkedList 链表Array 数组set集合
ArrayDeque 双端队列map 图
SequentialList 顺序表Queue 队列
package NEW_DATE_SEQUENCE_PACKAGE;import java.util.LinkedList;import java.util.ListIterator;/**
 *
 * @author cmx
 */public class J_8_31_1 
{
 public static void main(String[] args)
 {
 LinkedList<String> list=new LinkedList<>(); list.add("a"); list.add("b"); list.add("c"); ListIterator list3=list.listIterator(0); String get3=list.get(1); list3.next(); String test_2_move=list3.next().toString(); System.out.println("what>"+test_2_move); list3.set("hhhhh"); System.out.println(list.toString()); list.listIterator(0); int i =list.listIterator(0).nextIndex(); String a=list.get(0); System.out.println(a); String b=list.listIterator(1).previous(); System.out.println(b); System.out.println(i); ListIterator<String> list1=list.listIterator(1); String c=list1.next(); System.out.println(c); }
}/**
 * 
run:
what> b
[a, hhhhh, c]
a
a
0
hhhhh
成功构建 (总时间: 0 秒)
 */

In general, you know the roughly difference between arraylist and linkedlist:

1. Arraylist is a data structure that's based on dynamic arrays, linkedlist data structures.
2. For random access to get and set, arraylist looks better than linkedlist because linkedlist to move the pointer.
3. Linedlist is advantageous for adding and removing operatio & add and remove because the arraylist moves data.

Arraylist and KDM are two collection classes used to store a list of object references ( references ). For example, we can use arraylist to store a series of string or integer. What's the difference in the performance of the arraylist. When should you use an arraylist when it's used again.


,.
A key point is that the internal implementation of an arraylist is based on an array of objects, so it uses the get method to access any element in the list ( random access ), which is faster than the linkedlist. A get method in the is checked from one end of the list to the end of the list until the other end. In terms of mina, there's no faster way to access a specified element in the list.
Let's say that we've a large list of elements in which the elements are already listed. This list may be the type of arraylist, and now we've a binary search for the column table, and the comparison list is the query speed when the arraylist sum is evaluated by the comparison list:

Java code 收藏代码
  • packageCom. Mangocity. Test.
  • importLinkedlist java.util.;
  • importA java.util. list;
  • importjava.util. random;
  • importA java.util. arraylist;
  • importA java.util. array;
  • importjava.util. collections;
  • publicclassTestlist. %.
  • publicstaticfinalintN = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n _ 50000 ;
  • publicstaticList values;
  • static % 7b.
  • Integer vals [ ] = newInteger [ n ];
  • Random R = newRandom ( );
  • for( intI = 0 currval = 0 I <n; I + + ). %.
  • Vals = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = _ newInteger ( currval );
  • Currval + = R. Nextint (. 100 + ) 1 ;
  • %.
  • Value = array. AsList ( vals ).
  • %.
  • staticlongTimelist ( list lst ). %.
  • longStart = system. CurrentTimeMillis ( ).
  • for( intI = 0 I <n; I + + ). %.
  • intIndex = collections. Binarysearch ( lst, values, get ( I ) ).
  • if( index! = I )
  • System. Out. (. "* * * error * * *" );
  • %.
  • returnSystem. CurrentTimeMillis ( ) -start.
  • %.
  • publicstaticvoidMain ( string args [ ] ). % 7b.
  • System. Out. (. "arraylist co umption time:" + timelist ( newThe arraylist ( values ) );
  • System. Out. (. "linkedlist time:" + timelist ( new( value ) ) );
  • %.
  • %.

My output is: Arraylist co umption time: 15

Linkedlist time: 2596

This result isn't fixed, but basically the time of the arraylist is less than the linkedlist time. So in this case, linkedlist shouldn't be used. Random access ( random access ) policy used by the binary lookup method, which isn't supported by linkedlist random access. The amount of time to do random access to a linkedlist is proportional to the size of this list. As appropriate, the time spent in random access in an arraylist is a fixed time.

This indicates that the arraylist is always better than linkedlist performance. This doesn't necessarily, in some cases, to be better than arraylist, and some algorithms are more efficient when they're implemented in gorm. For example, when you reverse the list by using collections, reverse methods, the performance is better.

Look at an example, if we've a list of insert and delete operations, in this case, linkedlist is a good choice. Look at the extreme examples below, and we repeat an element at the beginning of a list:

Java code 收藏代码
  • packageCom. Mangocity. Test.
  • importA java.util. *;
  • publicclass7b ListDemo %
  • staticfinalintN = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n = n _ 50000 ;
  • staticlongTimelist ( list list ) %
  • longStart = system. CurrentTimeMillis ( ).
  • Object o = newObject ( );
  • for( intI = 0 I <n; I + + )
  • List. Add (. 0 O );
  • returnSystem. CurrentTimeMillis ( ) -start.
  • %.
  • publicstaticvoidMain ( string [ ] args ) % 7b
  • System. Out. (. "arraylist takes time:" + timelist ( newThe arraylist ( ) );
  • System. Out. (. "linkedlist time:" + timelist ( newLinkedlist ( ) )
  • %.
  • %.

At this point my output is: Arraylist takes time: 2463

Linkedlist time: 15

This is the opposite of the previous example. When an element is added to the end of an arraylist, all existing elements will move back, which means the overhead of data movement and replication. Instead, add an element to the beginning of the linkedlist simply not to assign a record to the element, and then adjust the two connections. Increasing the cost of an element at the beginning of the KDM is fixed, while increasing the cost of an element at the beginning of the arraylist is proportional to the size of the arraylist.


,,.
There's a private internal class in the textrange, as follows:

Java code 收藏代码
  • privatestaticclassEntry %
  • Object element;
  • Entry next;
  • Entry previous;
  • %.

One element in each entry object reference list, along with the previous element and the next element in the KDM. An object with 1000 elements will have 1000 linked entry objects, each of which corresponds to an element in the column table. In this case, there's a large amount of space overhead in a linkedlist structure because it stores information about the 1000 entity objects.
Arraylist stores the element using a built-in array, which is 10. When the array needs to grow, the new capacity is obtained as follows: New capacity = ( old capacity * 3 )/2 + 1, which means that each capacity will increase 50 %. This means that if you've an arraylist object that contains a large number of elements, there will be a huge amount of space that will be wasted, which is caused by. If there isn't enough space to hold new elements, the array will have to be so that new elements can be added. arrays will cause performance degradation. If we know how many elements an arraylist will have, we can specify the capacity by using the constructor. We can also remove wasted empty space after the arraylist is allocated by trimToSize method.
Between.


Three. Summary.
The arraylist and KDM have both advantages and disadvantages in performance, where they're all applicable and can be described as follows:
1. The cost of adding an element at the end of the list is fixed for arraylist and KDM. For arraylist, it's primarily added to an internal array, pointing to the added element, and occasionally may cause the array to be redistributed; This overhead is uniform, and an internal entry object is assigned to the KDM.


2. Inserting or removing an element in the middle of an arraylist means that the remaining elements in this list are moved. The cost of inserting or deleting an element in the middle of a KDM is fixed.


3. Doesn't support efficient random element access.


The space wasted in 4. Arraylist is mainly reflected in reserved capacity space at the end of the list list, and the linkedlist space takes a considerable amount of space in each element.


It can be said that using arraylist provides a better performance when the operation is added to the end of a column of data rather than in front or middle, and need to When your operation is to add or remove data in the front or middle of a column of data, you should use linkedlist when you access the elements in order.



arraylist ( dynamic array list ) diffe from array ( arrays )

1The variable of the array type must be instantiated at the same time. ( at least the size of the array is initialized ), and the arraylist can only be declared first.
2Array can store only isomorphic objects, while arraylist can store heterogeneous objects.
An [ object is an object that's the same type; if an array is declared as an int ], only the data is stored,
stringA [ ] can only hold character typed data, except for an array of object [ ].
The arraylist can hold any different type of data ( because it's stored in a loaded object object, in fact, it's used in an arraylist )
"Object [ ] _items;"This is a private field to encapsulate the object

3How to store in clr managed pai &
Array is always stored continuously, but the store doesn't have a continuous.

4Initialization size
The initialization of the array object must only specify the size, and the size of the array is fixed, and the
The size of the arraylist can be specified dynamically, and its size can be specified at initialization or not specified, that's, the space for that object can be.

5Array can't add and remove items at random, while arraylist can insert and delete items anywhere.


Similar points for array and arraylist
1An index ( index ), that's, can be obtained and modified directly by index.
2The objects they've created are placed in the managed heap.
3Can be enumerated on itself ( because the ienumerable interface is implemented ).

Some properties of arraylist

The capacity property value of an arraylist changes with the actual size of an item in an arraylist

By using the trimtoresize ( ) method of the arraylist class, you can remove empty items from an arraylist instance to compress the volume.
In c # 2.0, we recommend that you try to use the version of arraylist, which is the list <t> under.
This not only guarantees the type safety, but also improves the efficiency of the object processing because of the lack of packing.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs