Write a Java program to create a vector to store different types of objects


The code you provided demonstrates the usage of a Vector to store objects of different data types, including an instance of a custom class called Complex. Here's a step-by-step explanation of the code:

  • import java.util.*;: This line imports the java.util package, which contains the Vector class and other utility classes.
  • public class Different_Datatypes: This line declares a public class named Different_Datatypes.
  • 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 vec_list = new Vector();: This line declares and initializes a Vector object named vec_list. Note that it does not specify any generic type, making it a raw Vector that can store objects of any type.
  • vec_list.add(new Complex(10, 11));: This line creates a new Complex object with the values 10 and 11 for the real and imaginary parts, respectively. The object is then added to the vector vec_list. This process is repeated for the next four elements, which include integers, doubles, and booleans.
  • for (Object o: vec_list): This line starts a for-each loop that iterates over each element o in the vector vec_list. Since vec_list is a raw Vector, the elements are of type Object.
  • if (o instanceof Complex): Inside the for-each loop, this line checks if the current element o is an instance of the Complex class using the instanceof operator.
  • ((Complex) o).printComplex();: If the element is an instance of Complex, this line casts o to type Complex and calls the printComplex() method on it. This prints the real and imaginary parts of the complex number.
  • System.out.println(o + "");: If the element is not an instance of Complex, this line converts it to a string representation using concatenation with an empty string and prints it. This ensures that elements of different data types are displayed.
  • The Complex class is defined outside the main method. It contains two instance variables: real and ima, which represent the real and imaginary parts of a complex number.
  • Complex(int r, int i) : This is the constructor of the Complex class. It takes two integer parameters, r and i, and assigns them to the real and ima instance variables, respectively.
  • void printComplex(): This method prints the complex number in the format "real + ima i", where real and ima are the values of the corresponding instance variables.

This indicates that the vector contains objects of different data types: Complex objects, integers, doubles, and booleans. The appropriate method is called to display the Complex objects, while the other data types are printed as their string representations.

Source Code

import java.util.*;
public class Different_Datatypes
{
	public static void main(String[] args)
	{
		Vector vec_list = new Vector();
 
		vec_list.add(new Complex(10, 11));
		vec_list.add(new Complex(20, 21));
		vec_list.add(10);
		vec_list.add(20.5);
		vec_list.add(true);
 
		for (Object o: vec_list)
		{
			if (o instanceof Complex)
			{
				((Complex) o).printComplex();
			}
			else
			{
				System.out.println(o + "");
			}
		}
	}
}
class Complex
{
	int real;
	int ima;
 
	Complex(int r, int i)
	{
		this.real = r;
		this.ima = i;
	}
 
	void printComplex()
	{
		System.out.println(real + " + " + ima + "i");
	}
}

Output

10 + 11i
20 + 21i
10
20.5
true

Example Programs