Several methods of list in Java

Source: Internet
Author: User

Add () method. Insert data from a location, he has two parameters one parameter is the subscript, and one parameter is the element. It is important to note that the subscript size should be less than or equal to the list collection size, or the subscript out-of-bounds exception will be thrown!

Code: public static void Main (string[] args) {
TODO auto-generated Method Stub
ArrayList list=new ArrayList ();
List.add ("a");
List.add ("B");
List.add ("C");
List.add ("D");
List.add (4, "Z");
SYSTEM.OUT.PRINTLN (list);

}

Operating results: [A, B, C, D, z]

The Remove () method is removed by subscript. It is important to note that the delete operation is not automatically boxed, only the element where the subscript is located is deleted.

Get () method, gets the element at the specified position of the list, and we can iterate through the elements in the collection through Get ().

Code: public static void Main (string[] args) {
TODO auto-generated Method Stub
ArrayList list=new ArrayList ();
List.add ("a");
List.add ("B");
List.add ("C");
List.add ("D");
for (int i=0;i<list.size (); i++) {

System.out.println (List.get (i));

}


}

Operation Result:

A
B
C
D

Set () method to modify the elements of the specified subscript position to other elements.

Several methods of list 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.