将元素从一个向量移动到另一个向量 封装测试; 导入java.util.Random; 导入java.util.Vector; 公共类Variable2Variable{ 公共静态void main(字符串[]args){ 矢量线; 矢量线点; 随机rndNumbers=新随机数(); 直线=新向量(); linePoints=新向量(); 对于(int i=0;i
请运行上述代码,记录结果,然后取消对行“//linePoints.clear();”的注释,然后再次运行 如何将元素从矢量“线点”移动并存储到矢量“线”中? 谢谢。代替将元素从一个向量移动到另一个向量 封装测试; 导入java.util.Random; 导入java.util.Vector; 公共类Variable2Variable{ 公共静态void main(字符串[]args){ 矢量线; 矢量线点; 随机rndNumbers=新随机数(); 直线=新向量(); linePoints=新向量(); 对于(int i=0;i,java,Java,请运行上述代码,记录结果,然后取消对行“//linePoints.clear();”的注释,然后再次运行 如何将元素从矢量“线点”移动并存储到矢量“线”中? 谢谢。代替 package testing; import java.util.Random; import java.util.Vector; public class Variable2Variable { public static void main(String[] args) { Vector&l
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);
}
}
这样做:
lines.add(linePoints);
//linePoints.clear();
System.out.println("Vector: lines: " + lines);
System.out.println("Vector: linePoints: " + linePoints);
行。添加(线点);
linePoints=新向量();
System.out.println(“向量:行:”+行);
System.out.println(“向量:线点:“+线点”);
还有一些注意事项:
- 你把OOP全搞错了。在语义上,可以使用非结构化整数列表作为整数对列表。这为它自己的班级尖叫李>
- 既然可以使用ArrayList,为什么还要使用向量呢?你需要他们的线程安全性吗
lines.add(linePoints);
linePoints = new Vector<Integer>();
System.out.println("Vector: lines: " + lines);
System.out.println("Vector: linePoints: " + linePoints);
lines=新向量()//创建包含整数列表的列表实例
linePoints=新向量()//创建包含整数列表的列表实例
对于(int i=0;i<5;i++){//用偶数个整数填充整数列表
添加(rndNumbers.nextInt(10));
添加(rndNumbers.nextInt(10));
}
线条。添加(线条点);
//添加指向“整数列表**实例**”的引用
//到整数列表实例的列表
//这里不允许复制!仍然只有两个列表,其中一个引用了另一个。
//要复制,您应执行以下操作:
//添加(新向量(线点));
linePoints.clear()//该死!这将从整数列表中删除整数
//由于线条点仅引用列表实例本身,
//现在有相同的空列表。。。
System.out.println(“向量:行:”+行)//空的
System.out.println(“向量:线点:“+线点”)//1元素,空列表
解释修复程序的工作原理:
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=新向量()//创建包含整数列表的列表实例
linePoints=新向量()//创建包含整数列表的列表实例
对于(int i=0;i<5;i++){//用偶数个整数填充整数列表
添加(rndNumbers.nextInt(10));
添加(rndNumbers.nextInt(10));
}
线条。添加(线条点);
//添加指向“整数列表**实例**”的引用
//到整数列表实例的列表
linePoints=新向量()//创建新的空列表,
//并将参照线点设置为指向该列表。
//原始实例被单独保留,行列表的引用仍然指向它
//现在内存中有3个列表实例:
//*新的空向量,线点引用它
//*矢量实例,行引用它
//*在“旧”向量实例中,行的第一个元素引用它
System.out.println(“向量:行:”+行)//空的
System.out.println(“向量:线点:“+线点”)//1元素:具有整数的列表
没有。我拒绝运行任何东西。我们大多数人都会这样做。将ResultTM和预期结果也放在这里。。。顺便问一下,为什么要清除线点
?您刚刚将该列表添加到obejct的“整数列表”行中。如果清理它,因为这是与放入行中的实例相同的实例,它将是空的。谢谢佩特卡…使用“linePoints=new Vector();”效果很好,因此将替换“linePoints.clear();”
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