我正在尝试使用以下代码更新 ArrayList
的现有值:
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
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 Two
but I got [Zero, One, New, Two, Three]
as the result, and I still have Two
.我想打印 [Zero, One, New, Three]
。我怎样才能做到这一点?
原文由 Kani 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用
set
方法将旧值替换为新值。