Answers for "java list change value at index"

2

arraylist replace value java

list.set(1,"new value");

//example ..

List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list); // [one,two,three]
list.set(1,"new");
System.out.println(list); //[one,new,three]
Posted by: Guest on August-07-2020
0

how to update a list index value in java

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> list = new ArrayList<>();
         
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
         
        System.out.println(list);
 
         
        //Replace C with C_NEW
 
 
        //1 - In multiple steps
         
        int index = list.indexOf("C");
         
        list.set(index, "C_NEW");
         
        System.out.println(list);
 
 
        //2 - In single step replace D with D_NEW
 
        list.set( list.indexOf("D") , "D_NEW");
 
        System.out.println(list);
    }
}
Posted by: Guest on March-24-2021

Code answers related to "java list change value at index"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language