use*_*883 2 java arraylist subtraction
这是一个例子:
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;
}
Run Code Online (Sandbox Code Playgroud)
我想复制值,然后从我的新arraylist减去4.我怎样才能成功?我是java的新手.我也有一个错误,如:The left-hand side of an assignment must be a variable它指的是nowe_pozycje.get(j) -=4;.
您将获得get()值,更改它,然后set()是新值:
for (int j = 0; j < position.size(); j++) {
new_position.set(j, new_position.get(j) - 4);
}
Run Code Online (Sandbox Code Playgroud)
另一种解决方案可能是跳过整个列表的复制,而是遍历原始列表,随时更改每个值,并将它们添加到新的List:
public static ArrayList<Integer> new_position = new ArrayList<Integer>();
for (Integer i: position) {
new_position.add(i - 4);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4165 次 |
| 最近记录: |