如何更换在java中的ArrayList元素的现有值 [英] How to replace existing value of ArrayList element in java
本文介绍了如何更换在java中的ArrayList元素的现有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我还是很新的Java编程,我试图用这个code更新数组列表的现有值
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<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 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..
推荐答案
使用的<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#set%28int,%20E%29\"><$c$c>set$c$c>方法用新的替换旧值。
Use the set
method to replace the old value with a new one.
list.set( 2, "New" );
这篇关于如何更换在java中的ArrayList元素的现有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文