Java Dynamic Array ArrayList, java array arraylist

Source: Internet
Author: User
Tags addall

Java Dynamic Array ArrayList, java array arraylist
1. Class Structure:

· Java. lang. Object

· Java. util. AbstractCollection <E>

· Java. util. AbstractList <E>

· Java. util. ArrayList <E>

·All Implemented Interfaces:

Serializable, Cloneable, Iterable <E>, Collection <E>, List <E>, RandomAccess

2. Class Overview:

ListThe implementation of variable array of interface size. All optional list operations are implemented, andNullAll elements. Besides implementationListIn addition, this class also provides some methods to internally store the size of the List array. (This is equivalentVectorClass, except this class is not synchronized .)

Each ArrayList instance has a capacity. This capacity refers to the size of the array used to store list elements. It is always equal to at least the size of the list. As elements are added to the ArrayList, their capacity increases automatically.

Note: This implementation is not synchronous. If multiple threads access an ArrayList instance at the same time, and at least one thread modifies the list from the structure, it must maintain external synchronization. (Structure Modification refers to any operation to add or delete one or more elements, or to explicitly adjust the size of the underlying array. Only setting the element value is not a structure modification .) This is generally done by synchronizing the objects that encapsulate the list. If such an object does not exist, use the Collections. synchronizedList method to "Wrap" the list. It is best to complete this operation at the time of creation to prevent accidental access to the list:

 

3. Constructor

Public Constructors

 

ArrayList (int capacity)

Creates an empty list with the specified initial capacity.

 

ArrayList ()

Creates an empty list with an initial capacity of 0.

 

ArrayList (Collection <? Extends E> collection)

Construct a list of elements that contain the specified collection. These elements are arranged in the order returned by the collection iterator.

4. Common Methods:

 

 

Public Methods

Boolean

Add (E object)

Add the specified element to the end of the list.

Void

Add (int index, E object)

Insert the specified element into the specified position in this list.

Boolean

AddAll (Collection <? Extends E> collection)

Adds the objects in the specified collection to thisArrayList.

Boolean

AddAll (int index, Collection <? Extends E> collection)

Inserts all elements in the specified collection into the list starting from the specified position.

Void

Clear ()

Remove all elements from this list.

Boolean

Contains (Object object)

If the list contains the specified elementTrue.

Void

EnsureCapacity (int minimumCapacity)

AddArrayListThe capacity of the instance to ensure that it can accommodate at least the number of elements specified by the minimum capacity parameter.

E

Get (int index)

Returns the element at the specified position in the list.

Boolean

IsEmpty ()

Returns if thisCollectionContains no elements.

E

Remove (int index)

Removes an element from the specified position in the list.

Boolean

Remove (Object object)

Removes the specified element that appears for the first time in the list (if any ).

E

Set (int index, E object)

Replace the specified element with the specified position in the list.

Int

Size ()

Returns the number of elements in the list.

<T> T []

ToArray (T [] contents)

Returns an array containing all elements in the list in an appropriate order (from the first to the last element). returns the runtime type of the array, which is the runtime type of the specified array.

Object []

ToArray ()

Returns an array containing all elements in the list in the appropriate order (from the first to the last element.

Void

TrimToSize ()

Set thisArrayListThe instance capacity is adjusted to the current size of the list.

5. Differences between ArrayList (dynamic array) and static Array

1. access efficiency: static arrays are higher than dynamic arrays.

2. scalability: Dynamic Arrays can expand the size, but static arrays cannot. When you need to modify the size of static arrays, you can only use the new keyword to create a new array, therefore, the efficiency of dynamic arrays in expanding elements is relatively high.

3. Flexibility: Dynamic Arrays support set (replace), contains (whether to include an element), add (append element), and remove (remove element), but static arrays do not. Therefore, static arrays are more flexible than dynamic arrays.

 

 

 


Java Dynamic Array arraylist

Because your list is an empty list, the for Loop will not be executed.

Java Dynamic Array

ArrayList is an array that can be converted (you can understand)
ArrayList <E> where E is a generic type, for example, ArrayList <String> array = new ArrayList <String> (); indicates creating a new variable length array for storing strings.
ArrayList <Object> array1 = new ArrayList <Object> () is a variable length array that stores objects.
Remember, some E in this style are represented by T, which means generic. I will not introduce what generics are.
In addition, E can only be the class name of one class. If you change it to ArrayList <int>, it is incorrect. You can use ArrayList <Integer>.

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.