The code demonstrates how to create and use objects of a user-defined class in a LinkedList in Java. The Create_Object class contains a main method where a LinkedList object named l is created, and five Complex objects are added to it using the add method. The Complex class is a user-defined class representing complex numbers, with two integer fields re and img representing the real and imaginary parts of the complex numbers, respectively.
The System.out.println statement is used to print the message "LinkedList .. ". A for loop is used to iterate through the LinkedList and retrieve the Complex objects using the poll method, which retrieves and removes the first element from the LinkedList. The real and imaginary parts of each Complex object are then printed using the System.out.println statement.
The Complex class is defined separately from the Create_Object class, and it has a constructor that takes two integer arguments representing the real and imaginary parts of the complex number and initializes the corresponding fields re and img.
import java.util.*; public class Create_Object { public static void main(String[] args) { int i = 0; LinkedList <Complex> l = new LinkedList <Complex>(); l.add(new Complex(10, 20)); l.add(new Complex(20, 30)); l.add(new Complex(30, 40)); l.add(new Complex(40, 50)); l.add(new Complex(50, 60)); System.out.println("LinkedList .. "); for (i = 0; i < 5; i++) { Complex c = l.poll(); System.out.println(c.re + " + " + c.img + "i"); } } } class Complex { int re, img; Complex(int r, int i) { re = r; img = i; } }
LinkedList .. 10 + 20i 20 + 30i 30 + 40i 40 + 50i 50 + 60i
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions