将元素从一个向量移动到另一个向量
问题描述:
package testing;
import java.util.Random;
import java.util.Vector;
public class Variable2Variable {
public static void main(String[] args) {
Vector<Vector<Integer>> lines;
Vector<Integer> linePoints;
Random rndNumbers = new Random();
lines = new Vector<Vector<Integer>>();
linePoints = new Vector<Integer>();
for(int i = 0; i < 5; i++){
linePoints.add(rndNumbers.nextInt(10));
linePoints.add(rndNumbers.nextInt(10));
}
lines.add(linePoints);
//linePoints.clear();
System.out.println("Vector: lines: " + lines);
System.out.println("Vector: linePoints: " + linePoints);
}
}
请运行上面的代码,记下结果,然后取消注释行“//linePoints.clear();”然后再运行一次。将元素从一个向量移动到另一个向量
我怎样才能从矢量'linePoints'中移动元素并将它们存储到矢量'线'中? 谢谢。
答
而不是
lines.add(linePoints);
//linePoints.clear();
System.out.println("Vector: lines: " + lines);
System.out.println("Vector: linePoints: " + linePoints);
这样来做:
lines.add(linePoints);
linePoints = new Vector<Integer>();
System.out.println("Vector: lines: " + lines);
System.out.println("Vector: linePoints: " + linePoints);
也有一些注意事项:
- 你正在做的OOP都错了。您可以将非结构化的Integers列表语义上用作Integers对的列表。这为自己的班级尖叫!
- 为什么使用向量,当你可以使用ArrayLists呢?你需要他们的线程安全的性质?
TL; DR部分
你写的代码的说明:
lines = new Vector<Vector<Integer>>(); //create list instance holding list of Integers
linePoints = new Vector<Integer>(); //create list instance holding list of Integers
for(int i = 0; i < 5; i++){ // fill list of Integers with even number of Integers
linePoints.add(rndNumbers.nextInt(10));
linePoints.add(rndNumbers.nextInt(10));
}
lines.add(linePoints);
//add a reference pointing at the "list of Integers **instance**"
//to the list of list of Integers instance
//No copying happens here! Still only two lists, one has a reference to the other.
//to copy, you should do:
//lines.add(new Vector<Integer>(linePoints));
linePoints.clear(); //blam! this removes the integers from the list of Integers
// as linePoints has only a reference to the list instance itself,
// that has now the same empty list...
System.out.println("Vector: lines: " + lines); //empty
System.out.println("Vector: linePoints: " + linePoints); //1 element, the empty list
的修复是如何工作的说明:
lines = new Vector<Vector<Integer>>(); //create list instance holding list of Integers
linePoints = new Vector<Integer>(); //create list instance holding list of Integers
for(int i = 0; i < 5; i++){ // fill list of Integers with even number of Integers
linePoints.add(rndNumbers.nextInt(10));
linePoints.add(rndNumbers.nextInt(10));
}
lines.add(linePoints);
//add a reference pointing at the "list of Integers **instance**"
//to the list of list of Integers instance
linePoints = new Vector<Integer>(); //create new empty list,
//and set the reference linePoints to point to that list.
//the original instance is left alone, lines list's reference still points to it
//there are now 3 instances of List in memory now:
// * new empty Vector<Integer>, linePoints references it
// * the Vector<Vector<Integer>> instance, lines references it
// * the "old" Vector<Integer> instance, the first element of lines references it
System.out.println("Vector: lines: " + lines); //empty
System.out.println("Vector: linePoints: " + linePoints); //1 element: the list, having the integers
+0
谢谢ppeterka ....使用“linePoints = new Vector
号我拒绝任何运行。我们大多数人都会这样做。把结果,以及预期的结果放在这里......顺便说一句,你为什么要清除'linePoints'?您只是将该列表添加到“完整列表列表”对象的行中。如果你清理它,因为这是_same instance_作为你放入'lines'的那个,它将是空的。 – ppeterka 2013-02-20 14:02:03