Arraylist in Java

Source: Internet
Author: User

The arraylist class implements the list interface, which is a sub-interface of the collection interface and mainly adds methods for retrieving Objects Based on indexes.

 

Arraylist uses arrays to implement the list interface.For quick object acquisition, good performance can be achieved. However, when you delete an object or insert an object
,Especially when an object is inserted or deleted at the front end of the arraylist, the objects behind the internal array must be moved in sequence at a relatively high cost. Relatively slow
(Using the consumer list is better in this regard ).

 

There is a big difference between arrays and array lists. Arrays are a feature of the Java language. For each element type T, there is an array type T []. However, the arraylist class is a definition.
Class Library in the Java. util package, which is a "common" type for storing object type Elements. Note: To extract elements from the array list, type conversion is required.

 

The add () method can add new elements to the array list:

List list = new arraylist ();

List. Add (new element (......));

List. Add (new element (......));

The arraylist class manages the internal array referenced by an object and may use up the array space. If add is called and the internal array is full, the array list will automatically create a larger array and automatically copy the objects in the small array to the large array.

 

The Set () method can be used to set the element values in the array list:

List. Set (I, new element (......));

 

The get () method can get the elements in the array list. The returned type is object.

Element E = (element) list. Get (I );

 

The size () method returns the length of the current arraylist.

List. Size ();

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.