这是一个例子:
public static ArrayList<Integer> position = new ArrayList<Integer>();
public static ArrayList<Integer> new_position = new ArrayList<Integer>();
Collections.copy(new_position, position);
for (int j = 0; j < position.size(); j++) {
new_position.get(j) -=4;
}
我想复制值,然后从我的新数组列表中减去 4。我该怎么做?
我是 Java 的新手。我还有一个错误,例如:赋值的左侧必须是一个变量
,它指的是nowe_pozycje.get(j) -=4;
.
最佳答案
您必须get()
值,更改它,然后set()
新值:
for (int j = 0; j < position.size(); j++) {
new_position.set(j, new_position.get(j) - 4);
}
另一种解决方案可能是跳过列表的整个复制,而是遍历原始列表,随时更改每个值,并将它们添加到新的 List
:
public static ArrayList<Integer> new_position = new ArrayList<Integer>();
for (Integer i: position) {
new_position.add(i - 4);
}
关于java - 如何从 arraylist 中减去一些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22895991/