Write a Java program to create a vector to store integer elements


The code you provided is written in Java and creates a Vector object called vec_list that stores a list of integers. It then adds numbers from 10 to 15 (inclusive) to the vector using a for loop. Finally, it prints the elements of the vector. Here's the explanation of the code step by step:

  • import java.util.*;: This line imports the java.util package, which contains the Vector class and other utility classes.
  • public class Create_Vector: This line declares a public class named Create_Vector.
  • public static void main(String[] args): This is the main method where the execution of the program starts. It takes an array of strings as command line arguments.
  • Vector<Integer> vec_list = new Vector<Integer>();: This line declares and initializes a Vector object named vec_list that can store Integer values. Note that using generics (<Integer>) is not necessary in newer versions of Java.
  • for (int i = 10; i <= 15; i++): This line starts a for loop with the loop variable i initialized to 10, and it continues as long as i is less than or equal to 15. After each iteration, i is incremented by 1.
  • vec_list.add(i); : Inside the for loop, this line adds the value of i to the vector vec_list.
  • System.out.println("Vector Elements :" + vec_list);: Finally, this line prints the elements of the vector by concatenating the string "Vector Elements :" with the vector vec_list. The println function automatically converts the vector to a string representation.

Source Code

import java.util.*;
public class Create_Vector
{
	public static void main(String[] args)
	{
		Vector <Integer> vec_list = new Vector <Integer>();
 
		for (int i = 10; i <= 15; i++)
		{
			vec_list.add(i);
		}
		System.out.println("Vector Elements :" + vec_list);
	}
}

Output

Vector Elements :[10, 11, 12, 13, 14, 15]

Example Programs