Get/set element

ReturnMethodSummary
Eget(int index)Returns the element at the specified position in this list.
Eset(int index, E element)Replaces the element at the specified position in this list with the specified element (optional operation).

import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List list = new ArrayList();

    list.add("1");
    list.add("2");
    list.add("3");

    list.add("java2s.com");

    System.out.println(list);

    System.out.println(list.get(3));


  }
}

The output:


[1, 2, 3, java2s.com]
java2s.com

Replace the third element in a list


import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List list = new ArrayList();

    list.add("1");
    list.add("2");
    list.add("3");

    list.add("java2s.com");

    System.out.println(list);

    list.set(2, "java 2s .com");
    System.out.println(list);


  }
}

The output:


[1, 2, 3, java2s.com]
[1, 2, java 2s .com, java2s.com]
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.