我想将输入整数的数组转换为向量,然后输出结果。我搜索谷歌和每个例子使用“(Arrays.asList(randomArray)”。但是,当我尝试编译我得到一个“无法找到符号 - 构造函数向量(java.util.list)”什么是正确的代码?一个数组转换为矢量如何将整数数组转换为矢量?
这里是我的代码:
Scanner inputNumber = new Scanner(System.in);
System.out.println("How big would you like the vector to be?");
int vecSize = inputNumber.nextInt();
int [] vecArray = new int[vecSize];
int [] primeArray = new int[vecSize];
System.out.println("Please enter " + vecSize + " postive numbers please: ");
for (int i = 0; i < vecSize; i++) {
int arrayInput = inputNumber.nextInt();
if (arrayInput > 0){
vecArray[i] = arrayInput;
}
}
Vector<Integer> arrayToVec = new Vector<Integer>(Arrays.asList(vecArray));
你为什么要使用矢量?如果你想同步,使用'Collections.synchronizedList'。 – zengr
...如果你不需要线程安全同步,你可以使用'ArrayList'或者'Arrays.asList()' –