how to update a list index value in java

Solutions on MaxInterview for how to update a list index value in java by the best coders in the world

showing results for - "how to update a list index value in java"
Ben
20 Jan 2021
1public class ArrayListExample 
2{
3    public static void main(String[] args) 
4    {
5        ArrayList<String> list = new ArrayList<>();
6         
7        list.add("A");
8        list.add("B");
9        list.add("C");
10        list.add("D");
11         
12        System.out.println(list);
13 
14         
15        //Replace C with C_NEW
16 
17 
18        //1 - In multiple steps
19         
20        int index = list.indexOf("C");
21         
22        list.set(index, "C_NEW");
23         
24        System.out.println(list);
25 
26 
27        //2 - In single step replace D with D_NEW
28 
29        list.set( list.indexOf("D") , "D_NEW");
30 
31        System.out.println(list);
32    }
33}
34