Updating Elements in ArrayList with set method - Java Collection Framework

Java examples for Collection Framework:ArrayList

Introduction

You can use the set method to replace an existing object with another object. For example:

Demo Code

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> nums = new ArrayList<String>();
    nums.clear();/*from w  w  w. ja  va 2s . c  o m*/
    nums.add("One");
    nums.add("Two");
    nums.add("Three");
    
    System.out.println(nums);
    
    nums.set(0, "Uno");
    nums.set(1, "Dos");
    nums.set(2, "Tres");
    
    System.out.println(nums);

  }

}

Related Tutorials