Java Basics Essay 1

Source: Internet
Author: User

Recently ready to move, may want to do javaweb from Andro, seize the time to review the basic Java content. Just find Crossoverjie's Java-interview project on GitHub, and learn it all by yourself.

————————————————————————————————————————————————————————————————————————————

The underlying analysis of Arraylist/vector ArrayList

ArrayListImplemented in List , RandomAccess interface. You can insert empty data and also support random access.

ArrayListEquivalent to Dynamic Data, where the two most important attributes are: elementData array, and size size. At add() the time of calling the method:

    Boolean Add (e e) {        1);  //increments modcount!! elementdata[sizetrue;}   
    • First, the expansion check.
    • Place the inserted value at the tail and size + 1.

If the call is add(index,e) added at the specified location:

    void Add (E Element) {        Rangecheckforadd (index);        System= element; size+ +;}   
    • Also the first expansion check.
    • Then the data is copied, the purpose is to empty the index position to put the inserted data, and the back of the data to move backward one position.

In fact, the expansion of the final call code:

    Privatevoid Grow (int mincapacity) {Overflow-conscious Codeint oldcapacity = elementdata.length; int newcapacity = oldcapacity + (oldCapacity >> 1); if (newcapacity -mincapacity < 0) Newcapacity = mincapacity; if (newcapacity -max_array_size > 0) newcapacity = hugecapacity (mincapacity); //mincapacity is usually close to size, so this is a win:elementdata = arrays.copyof (Elementdata, newcapacity);}      

is also a process of copying an array.

ArrayListThis shows that the main consumption is the expansion of the array and in the specified location to add data, in daily use is best to specify the size, to minimize the expansion. The more you want to reduce the operation of inserting data at the specified location.

Serialization of

Since ArrayList is implemented based on dynamic arrays, not all of the space is used. Therefore, the transient modification is used to prevent automatic serialization.

Object[] Elementdata;

So ArrayList has customized serialization and deserialization:

    Privatevoid WriteObject (java.io. ObjectOutputStream s) throwsjava.io. ioexception{Write out element count, and any hidden stuffint Expectedmodcount= Modcount; S. Defaultwriteobject ();Write out size as capacity-behavioural compatibility with clone () s. Writeint (size);Write elements in the proper order.Only the data that was used is serializedfor (int I=0; I<size; I+ +) {s. WriteObject (Elementdata[i]); }if (modcount! = Expectedmodcount) {ThrowNewConcurrentmodificationexception (); } }Privatevoid ReadObject (java.io. ObjectInputStream s) throwsjava.io. IOException,classnotfoundexception {Elementdata=Empty_elementdata;//Read in size, and any hidden stuff s.defaultreadobject (); //Read in capacity S.readint (); //ignored if (Size > 0) {//is like Clone (), allocate array based upon size not CAPA City ensurecapacityinternal (size); object[] a = elementdata; //Read in all elements in the proper order. for (int i=0; I <size; I++) {A[i] = s.readobject ();}}      

When the WriteObject and ReadObject methods are customized in an object, the JVM invokes both of these custom methods for serialization and deserialization.

It can be seen from the implementation that ArrayList only serializes the data being used.

The above is a copy of git content. The following explains what I am not familiar with and understand.

————————————————————————————————————————————————————————————

First, ArrayList

The first is the ArrayList Foundation, ArrayList refers to the dynamic array, including three main advantages:

Dynamic increase and decrease of elements
Implements the ICollection and IList interfaces
Flexibility to set the size of the array

ArrayList has three constructors, namely public ArrayList (); Public ArrayList (ICollection); public ArrayList (int); (The first constructor is constructed by default at 16, the second adds the elements of the collection to ArrayList, and the third is constructed by parameters).

The IsSynchronized property indicates whether the current ArrayList instance supports thread synchronization, while the Arraylist.synchronized static method returns the encapsulation of a ArrayList thread synchronization. (Let's do this, the threading problem is explained in detail later)

The Trimsize method is used to pin the ArrayList to the actual element size, and when the dynamic array element is determined not to be added, this method can be called to free up free memory.

Efficiency issues:

1. Array expansion is one of the most important factors that affect the efficiency of ArrayList. Whenever you do add, AddRange, Insert, Insertrange, and so on, the internal array's capacity is not enough, it will rebuild an array at twice times the current capacity, copy the old elements into the new array, and discard the old array. If you do not run the Trimsize method or the storage element is not sufficient, it can cause significant memory waste.

2. Calling IndexOf, contains, and so on is a simple loop that executes to find elements that can cause inefficiencies and is less likely to be looked up by your own handwriting algorithm.

Second, List interface

Any data can be stored in the list interface. and the content in the list interface is allowed to be duplicated. The function of the list interface is much more powerful than the collection interface because of the extensive expansion of the operation of the collection interface. Extension method: 1, public void Add (int index,e element) normally adds elements at the specified position. 2. Public boolean addall (int index, COLLECTION&LT;? extends e> c) adds a set of elements to the specified position. 3, E get (int index) returns the element at the specified position. 4, public int indexOf (Object o) normally finds the position of the specified element. 5, public int lastIndexOf (Object o) normally finds the position of the specified element from the back forward. 6, Public listiterator<e> listiterator () Common for Listiterator interface instantiation. 7, public E Remove (int index) removes the element at the specified location. 8, public list<e> sublist (int fromIndex, int toindex) Normally take out the subset in the collection. 9, public E set (int index, E Element) replaces the element at the specified position. The Randomaccess interface randomaccess is a tagging interface that indicates that the list that implements the interface supports fast random access, primarily to enable the algorithm to perform more efficiently in random and sequential access lists.

Java Basics Essay 1

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.