Java advanced (24) Java List set add and set method principles

Source: Internet
Author: User

Java advanced (24) Java List set add and set method principles
Principles of the add and set methods in the JavaList set

The add method is used to add objects to the set list.

Syntax 1

Inserts a specified element at the end of the list. If the List set object is changed because the add method is called, true is returned; otherwise, false is returned. Add (Ee) e: Elements to be added to the list.

Instance
Public static void main (String [] args) {List

 
  
List = new ArrayList

  
   
(); List. add ("Protect the Environment"); // add a data list to the list. add (""); // add a data list to the list. add ("start from me"); // add data to the list // output the content in the list through a loop (int I = 0; I

   
    
Syntax 2

    

Insert a specified element at a specified position in the list, and Add 1 to the index of the element currently in this position and its subsequent elements. Voidadd (intindex, Eelement)

Index: used to insert the index at the specified element.

Element: Specifies the element to be inserted.

The index value starts from 0.

Set Method

The Set method is used to replace the elements in the Set list.

Voidset (intindex, Eelement)

Replace the element at the index with the element Object and return the new element.

Meiwenmeitu

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.