Java basics and java Basics

Source: Internet
Author: User

Java basics and java Basics


A set includes commonly used ArrayList, HashMap, and HashSet, as well as uncommonly used stacks and Queue, thread-safe Vector and HashTable, thread-unsafe failover list, And TreeMap.

  • Arraylist

ArrayList is a dynamic array that implements the List interface. The initial capacity is 10, that is, the array size. Each time an element is added to the container, the capacity check is performed. When the overflow is fast, the resizing operation is performed (data is re-copied to the new array ). It is not synchronous and used in multiple threads. It is best to ensure synchronization during creation.

For example:

 List list = Collections.synchronizedList(new ArrayList()); 

Create: Create a transient modified array.

New:

Method: add (E), add (int index, e element), addAll (Collection <? Extends E> c), addAll (int index, Collection <? Extends E> c), set (int index, E element)

Generally, the container size is checked first to check whether expansion is required. Call the ensureCapacity () method to resize the ArrayList set.

Public void ensureCapacity (int minCapacity) {// modify the timer modCount ++; // ArrayList capacity size int oldCapacity = elementData. length;/** if the length required is greater than the length of the current array, perform the expansion operation */if (minCapacity> oldCapacity) {Object oldData [] = elementData; // calculate the new capacity, which is 1.5 times the current capacity. int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity <minCapacity) newCapacity = minCapacity; // copy the array to generate the new array elementData = Arrays. copyOf (elementData, newCapacity );}}

If you insert an element add (int index, E element) at the specified index position, System is called. the arraycopy () method is used to leave the index position blank for new data insertion. Here, the right shift of array data is required, which is very troublesome and time consuming, therefore, if the specified data set requires a large number of insert operations, we recommend that you use the sequence list.

Delete:

Method: remove (int index), remove (Object o), removeRange (int fromIndex, int toIndex), removeAll ()

After the element is deleted, the System. arraycopy (...) method is called to adjust the position of the remaining element.

Obtain:

Method: get (int index)

Because ArrayList is a dynamic array, we can obtain the elements in ArrayList Based on the subscript, and the speed is relatively fast. Therefore, ArrayList is suitable for random access.

  • Shortlist

The random List is a linked List that implements the List interface. It is superior to ArrayList when inserting or deleting data, while random access is inferior to ArrayList.

 



 

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.