I am still quite new to java programming and I am trying to update existing value of Array list by using this code
public static void main(String[] args){
List list = new ArrayList();
list.add( "Zero" );
list.add( "One" );
list.add( "Two" );
list.add( "Three" );
list.add( 2, "New" ); // add at 2nd index
System.out.println(list);}
I want to print New instead of Tow but I got [Zero, One, New, Two, Three] as the result, I still have "Two". I want to print [Zero, One, New, Three]. How do I do this?
Thank You..
解决方案
Use the set method to replace the old value with a new one.
list.set( 2, "New" );