Data for container types in Java

Source: Internet
Author: User

Stingbiulder, Array, ArrayList

Stringbiulder: variable length for string only.

Array: The length of the array is fixed.

ArrayList: variable length.

Construction Method:

  ArrayList ();

To add an element:

Public boolean Add (E E): add element

public void Add (int index,e element): Adding elements at the specified index

ArrayList (E): implementation of variable size arrays

To create a collection object:

arraylist<string> array = new arraylist<string> ();

ArrayList the additions and deletions to change:

1. Adding elements

Array.add ("Hello");

Array.add ("World");

Array.add ("Java");

2. Get elements

Public E get (int index): Returns the element at the specified index

Usage Example: Array.get (0)//run result is Hello

3. Set length

public int size (): Returns the number of elements in the collection

usage example; array.size ();//operation result is 3;

4. Deleting an element

A. Public boolean remove (Object o);//delete the specified element and return whether the deletion succeeded

Usage Example: Array.remove ("Hello");//operation result is ture;

B. public E Remove (int index);//delete the element at the specified index and return the deleted element

Usage Example: Array.remove (1);//operation result is world;

5. Modifying elements (replacing elements)

Public E Set (int index, E Element);//Modify the element at the specified index to be another element (that is, replace) and return the modified element

Usage Example: Array.set (1,android);//operation result is world;

Data for container types in Java

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.