"Go" Java.util.ArrayList.set () Method instance

Source: Internet
Author: User

java.util.ArrayList.set (int index, E Element) replaces the element with the specified element at the specified position in this list.

Statement

The following is a declaration of the Java.util.ArrayList.set () method

Public E Set (int index, e Element)
Parameters
    • Index--The element that replaces the index.

    • Element--elements to be stored in the specified position.

return value

This method returns the element before the specified position.

Abnormal
    • Indexoutofboundsexception--If the index is out of range

Example

The following example shows the use of the Java.util.Arraylist.set () method.

Package Com.yiibai;import Java.util.arraylist;public class Arraylistdemo {public   static void Main (string[] args) { c1/>//create an empty ArrayList with an initial capacity      arraylist<integer> arrlist = new arraylist<integer& gt; (5);      Use Add () method to add elements in the list      arrlist.add (n);      Arrlist.add (a);      Arrlist.add (+);      Arrlist.add (a);      Let us print all the elements available in list for      (Integer number:arrlist) {        System.out.println ("number = "+ number);      }      Inserting elment at 3rd position      Arrlist.set (2,55);        Let us print all the elements available in list      System.out.println ("Printing new list:");      for (Integer number:arrlist) {        System.out.println ("number =" + number);}}}    

Let's compile and run the above program, which will produce the following results:

Number = 15Number = 20Number = 25Number = 22Printing new List:number = 15Number = 20Number = 55Number = 22

Transferred from: http://www.yiibai.com/java/util/arraylist_set.html

"Go" Java.util.ArrayList.set () Method instance

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.