I’m trying to turn an array of inputed integers to a vector, then output the result. I’ve searched google and every example uses the ” (Arrays.asList(randomArray)”. However, when i try to compile i get a “cannot find symbol – constructor Vector(java.util.list)” what is the correct code to convert an array to a vector?
here is my code:
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));
The problem is that you have an array of primitive type (int), which doesnt work well with
Arrays.asList().Arrays.asList(vecArray)actually returns aList<int[]>with one element (your array).The easiest fix is to populate the vector yourself manually: