I am making a simple program to understand how the vector class works, but when I run it the program does not show me or ask me for anything. Specifically, I don't know how to make the user enter a number by keyboard and it is saved as an element of my Vector class.
package entrenamiento.ahora.si.de.vectores.xd;
import java.util.Scanner;
import java.util.Vector;
/**
*
* @author Usuario
*/
public class EntrenamientoAhoraSiDeVectoresXD {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Vector numeros = new Vector (5,5);
Scanner entrada = new Scanner(System.in);
for(int i=0; i<numeros.size(); i++){
System.out.println("Ingrese un numero para agregarlo al vector");
numeros.addElement(i);
}
for(int i=0; i<numeros.size(); i++){
System.out.println("Los numeros ingresados al vector son ");
System.out.println("Elemento "+(i+1) + ": "+numeros.elementAt(i));
}
}
}
This shows me when it is running
run:
BUILD SUCCESSFUL (total time: 0 seconds)
your program does exactly what you see...nothing...
And this why? because to understand how something works, nothing better than reading the documentation of that something...
If we go to the documentation of the Vector class
And more precisely to the method you are using, it says:
In other words, you are creating an empty Vector, with a capacity of 5 elements at first, and that every time it reaches 5, it will increase the capacity by 5 more...
That's why size returns zero. It has no items.