Java 如何从arraylist中减去一些值?
以下是一个例子:Java 如何从arraylist中减去一些值?,java,arraylist,subtraction,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 < pos
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;
}
public static ArrayList position=new ArrayList();
public static ArrayList new_position=new ArrayList();
收藏。复制(新位置,位置);
对于(int j=0;j
我想复制值,然后从新的arraylist中减去4。我怎样才能做到?
我是java新手。还有一个错误,比如:赋值的左边必须是一个变量
,它指的是nowe\u pozycje.get(j)-=4
您必须获取()
值,更改它,然后设置()
新值:
for (int j = 0; j < position.size(); j++) {
new_position.set(j, new_position.get(j) - 4);
}
如果要从ArrayList的每个元素中减去4,则:
ArrayList<Integer> position = new ArrayList<Integer>();
ArrayList<Integer> new_position = new ArrayList<Integer>();
Collections.copy(new_position, position);
for (int j = 0; j < position.size(); j++) {
new_position.set(j, (new_position.get(j) - 4)); //remove 4 from element value
}
ArrayList position=new ArrayList();
ArrayList new_position=新ArrayList();
收藏。复制(新位置,位置);
对于(int j=0;j
您不需要使用Collection.copy()。获取值,从中减去,然后放回原处。
ArrayList
不是数组。subtract 4
的意思是什么?你到底想做什么?是否要删除4个元素?
ArrayList<Integer> position = new ArrayList<Integer>();
ArrayList<Integer> new_position = new ArrayList<Integer>();
Collections.copy(new_position, position);
for (int j = 0; j < position.size(); j++) {
new_position.set(j, (new_position.get(j) - 4)); //remove 4 from element value
}
for (int n : position) new_position.add(n-4);