如何改变在java中的ArrayList元素的值 [英] How to change value of ArrayList element in java
问题描述
请帮我下面code,我得到的结果相同,甚至修改完毕
Please help me with below code , I get the same output even after changing the value
import java.util.*;
class Test {
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<Integer>();
// added 0-9 to ArrayList
for(int i=0;i<9;i++)
a.add(new Integer(i));
// initialize the Iterator
Iterator<Integer> i = a.iterator();
// changed the value of first element in List
if(i.hasNext()) {
Integer x = i.next();
x = Integer.valueOf(9);
}
// initialized the iterator again and print all the elements
i = a.iterator();
while(i.hasNext())
System.out.print(i.next());
}
}
//Output : 012345678
值9没有更新。
推荐答案
列表被保持的对象引用存储在所述列表中的原始值。所以,当你执行这行:
The list is maintaining an object reference to the original value stored in the list. So when you execute this line:
Integer x = i.next();
两者 X
和列表中存储到同一个对象的引用。然而,当你执行:
Both x
and the list are storing a reference to the same object. However, when you execute:
x = Integer.valueOf(9);
一切都没有在列表中改变,但 X
现在存储到不同的对象的引用。名单没有改变。您需要使用一些列表操作方法,如
nothing has changed in the list, but x
is now storing a reference to a different object. The list has not changed. You need to use some of the list manipulation methods, such as
list.set(index, Integer.valueof(9))
请注意:这有的没有的做整数
的不变性,正如其他人所提出的建议。这仅仅是基本的Java对象的引用行为。
Note: this has nothing to do with the immutability of Integer
, as others are suggesting. This is just basic Java object reference behaviour.
下面是一个完整的例子,来帮助解释这一点。注意,这使得使用的<一href=\"http://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html\"><$c$c>ListIterator$c$c>类,它支持删除/设置项目中期迭代:
Here's a complete example, to help explain the point. Note that this makes use of the ListIterator
class, which supports removing/setting items mid-iteration:
import java.util.*;
public class ListExample {
public static void main(String[] args) {
List<Foo> fooList = new ArrayList<Foo>();
for (int i = 0; i < 9; i++)
fooList.add(new Foo(i, i));
// Standard iterator sufficient for altering elements
Iterator<Foo> iterator = fooList.iterator();
if (iterator.hasNext()) {
Foo foo = iterator.next();
foo.x = 99;
foo.y = 42;
}
printList(fooList);
// List iterator needed for replacing elements
ListIterator<Foo> listIterator = fooList.listIterator();
if (listIterator.hasNext()) {
// Need to call next, before set.
listIterator.next();
// Replace item returned from next()
listIterator.set(new Foo(99,99));
}
printList(fooList);
}
private static void printList(List<?> list) {
Iterator<?> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next());
}
System.out.println();
}
private static class Foo {
int x;
int y;
Foo(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return String.format("[%d, %d]", x, y);
}
}
}
这会打印:
[99, 42][1, 1][2, 2][3, 3][4, 4][5, 5][6, 6][7, 7][8, 8]
[99, 99][1, 1][2, 2][3, 3][4, 4][5, 5][6, 6][7, 7][8, 8]
这篇关于如何改变在java中的ArrayList元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!