with no parameters will use the default pass of a 10 parameter. An object array of length 10 is declared in the constructor with parameters./***thearraybufferintowhich theelementsofthearraylistarestored.* ThecapacityoftheArrayLististhelengthofthis arraybuffer.*/privatetransientobject[] Elementdata;publicarraylist (intinitialcapacity) { super ();if ( initialcapacityThe following is the Add method that we often use, which first determines whether the length of the underlying array is greater than
synchronization, that is, only one thread can write a Vector at a time point, avoid inconsistency caused by simultaneous write of multiple threads, but it is costly to implement synchronization. Therefore, it is slower to access ArrayList than to access ArrayList.
3. The linked list structure is used to store data. It is suitable for dynamic insertion and deletion of data, and the random access and travers
Always summarize the knowledge in the Java collection and don't know how to write. Think set too many things, write thin too difficult, write coarse and feel write bad. No matter how you feel or want to insist on writing the basics of this kind of things, in order to improve their programming base. Originally think of these already very familiar with these, recently saw some big God after found that the gap is too big, instant Meng, only in strengthen
multi-implementation, breaking a class can only inherit a parent class limit; ② provide rules ③ reduce the coupling of the program (can achieve modular development, each person to achieve their own modules, improve development efficiency)
Differences between interfaces and abstract classes: see notes for an in-depth understanding of abstract class and interface
Back to the chase.
The list is an interface, and the interface cannot be instantiated (without constructors), and
Arraylist can be seen as a packaging of an array to implement a variable array.1. Basic fields of arraylist
// You may be wondering why the array is marked as transient.// This array cannot be serialized during sample serialization. Therefore, you can only manually serialize and save the arraylist// Element value. Although all objects in the array are stored as r
Turn from:Java ArrayList source Code anatomyGeneral IntroductionArrayList implements the List interface, which is the sequential container, where the elements hold the same data as in the order in which they are placed, allowing the elements to be placed and the null underlying to be implemented by arrays . Except that the class does not implement synchronization, the rest is roughly the same as the Vector . Each
1. ArrayList Overview:
ArrayList is an array queue, which is the equivalent of a dynamic array. Its capacity can grow dynamically compared to an array in Java. It inherits from Abstractlist, implements the list, randomaccess, cloneable, java.io.Serializable these interfaces.ArrayList inherited the Abstractlist and realized the list. It is an array queue that prov
, where only one thread can write vectors at a time, avoiding inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList.
LinkedList is used to store data in a linked list structure, which is very suitable for the dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, he provide
, where only one thread can write vectors at a time, avoiding inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList.
LinkedList is used to store data in a linked list structure, which is very suitable for the dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, he provide
Translate from: How to Convert arrays to ArrayList in Java?This article analyzes the answer to one of the hottest questions on stack overflow, where the questioner gets a lot of prestige points, allowing him to get the right to do a lot of things on the stack overflow. It has nothing to do with me, let's take a look at it first.The question is "How do I convert an array to
from Destpos to Destpos+length-1. Because it is a native method, it is not visible in the JDK, but you can view the source code in the OPENJDK. The function actually eventually calls the C language's memmove () function, so it can guarantee the correct copying and movement of elements within the same array, which is much more efficient than the general method of replication, and is well suited for batch processing of arrays . Java strongly recommends
Java Container Class Collections ToArray () method, can transform such as arraylist
However, the conversion method is wrong.
String[] Strarray = (string[]) Arraylist.toarray ();If this execution results in the following error:
Exception in thread "XX" java.lang.ClassCastException: [Ljava.lang.Object; cannot being cast to [Ljava.lang.String;
The following figure:
The correct way should be written:
String[
one thread can write vectors at a time, avoiding inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList. 3.LinkedList is used to store data in a linked list structure, which is very suitable for the dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, he provides methods that
the pointing position. The removal is to move the back forward one bit directly, and the last position is null. From this mode of operation can be seen ArrayList features: for the query, it is very simple, loop array can be, and if you know the subscript, it is possible to directly return the array subscript. However, for the addition of the deletion, if it is in the middle operation, it will involve the movement of the array, it is more troublesome,
of the expansion space).ArrayList and vector The biggest difference is the use of synchronization synchronization,there is no method in ArrayList synchronization , and the majority of vectors are directly or indirectly synchronous , so vectors are thread-safe and ArrayList are not thread -safe, but also because of the vector's thread-safety mechanism, which make
synchronization, where only one thread at a time can write vectors. Avoid inconsistencies caused by multithreading at the same time, but achieving synchronization requires very high costs, so. It is slower to visit than to ask ArrayList.3.LinkedList is used to store data in a linked list structure, which is very suitable for dynamic insertion and deletion of data, and the random access and traversal speed is slower. In addition, he provides methods t
thread can write vectors at a time, avoiding inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList.3.LinkedList is used to store data in a linked list structure, which is very suitable for the dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, he provides methods that are
Same point:
All inherits from Abstractlist and implements the list interface
Both Randomaccess and cloneable interfaces are implemented.
The default array size is 10
All support iterator traversal
Different points:
ArrayList is non-thread safe;
and vector is thread-safe, its functions are synchronized, that is, all support synchronous
Serialization support differs:
Java Collection ArrayList-thoroughly understand additions and deletionsFrom the source to see the initialization and additions and deletions to learn ArrayList.Let's take a look at some of the properties defined by ArrayList:privatestaticfinalint10;privatestaticfinal Object[] EMPTY_ELEMENTDATA = {};privatestaticfinal Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};transient Object[] elementData; // 保存对象pri
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.