import java.util.*;
class Book
{
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity)
{
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class ListIteratorExample
{
public static void main(String[] args)
{
// Creating list of Books
List<Book> list = new ArrayList<Book>();
// Creating Books
Book b1 = new Book(101, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(102, "Java Program Questation", "Rajendra", "TechSarvam", 4);
Book b3 = new Book(103, "Operating System", "Galvin", "Wiley", 6);
// Adding Books to list
list.add(b1);
list.add(b2);
list.add(b3);
System.out.println("Original content of list is: ");
// Traversing list
for (Book b : list)
{
System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity);
}
// Using ListIterator to traverse the list in reverse order
ListIterator<Book> itr = list.listIterator(list.size());
System.out.println("Modified content of list in backward is: ");
while (itr.hasPrevious())
{
Book st = itr.previous();
System.out.println(st.quantity + " " + st.publisher + " " + st.author + " " + st.name + " " + st.id);
}
}
}
Step-by-Step Explanation:
1. Class Declaration (Book):
class Book {
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;}
this.publisher = publisher;
this.quantity = quantity;
}
}
- Book Class: This class is intended to encapsulate the book with fields such as id, name, author, publisher, and quantity.
- It has a constructor that takes the provided values to initialize these fields.
2. Main Method:
public class ListIteratorExample {
public static void main(String[] args) {
main() is the entry point of the program.
3. Creating a List of Books (ArrayList):
List<Book> list = new ArrayList<Book>();
An ArrayList of type Book is defined to store the books. The List interface is used for the possibility of any type of list implementation, such as an ArrayList in this case.
4. Creating Book Objects:
Book b1 = new Book(101, “Let us C”, “Yashwant Kanetkar”, “BPB”, 8);
Book b2 = new Book(102, “Java Program Question”, “Rajendra”, “TechSarvam”, 4);
Book b3 = new Book(103, “Operating System”, “Galvin”, “Wiley”, 6);
Three Book objects (b1, b2, and b3) are instantiated by calling the Book constructor. In each book, id, name, author, publisher, and quantity are used as arguments.
5. Adding Books to the List:
list.add(b1);
list.add(b2);
list.add(b3);
The add() method is used to add the created books (b1, b2, b3) to the ArrayList list.
6. Printing Original List of Books:
System.out.println(“Original content of list is: “);
for(Book b : list) {
System.out.println(b.id + ” ” + b.name + ” ” + b.author + ” ” + b.publisher + ” ” + b.quantity);
}
- The for-each loop is used to iterate over each Book object in the list.
- For each book, its id, name, author, publisher, and quantity are printed to the console.
Output of this section will be:
Original content of list is:
101 Let us C Yashwant Kanetkar BPB 8
102 Java Program Question Rajendra TechSarvam 4
103 Operating System Galvin Wiley 6
7. Using ListIterator for Backward Traversal:
ListIterator<Book> itr = list.listIterator();
System.out.println(“Modified content of list in backward is: “);
while(itr.hasNext()) {
Book st = itr.next();
System.out.println(st.quantity + ” ” + st.publisher + ” ” + st.author + ” ” + st.name + ” ” + st.id);
}
- A ListIterator for the list is returned by the method listIterator(). Both forward and backward traversals through the list are possible.
- The while loop iterates over the ListIterator of the list and prints quantity, publisher, author, name, and id for each of the Book.
- As ListIterator is being used in its forward mode, so list is traversed from beginning to end.
Output of this section will be:
Modified content of list in backward is:
8 BPB Yashwant Kanetkar Let us C 101
4 TechSarvam Rajendra Java Program Question 102
6 Wiley Galvin Operating System 103
Key Points:
1. ArrayList: This is an implementation of the list interface that allows dynamic resizing of the array. It stores elements in a sequential manner.
2. ListIterator: This is an interface in Java through which we can traverse a list in both the forward and backward direction. It also provides methods like:
- hasNext() for checking if there are more elements in the list.
- next() for retrieving the next element.
- You can also use previous() and hasPrevious() for backward traversal, but we are not using that in this code.
3. Printing Book Details: The program prints the Book details in two different ways:
- Initially, it prints the book details in the original order of insertion.
- Later, it prints the book details in a different order using the ListIterator, where the fields of the books are printed in a different sequence (quantity, publisher, author, name, id).
Full Output of the Program:
Original content of list is:
101 Let us C Yashwant Kanetkar BPB 8
102 Java Program Question Rajendra TechSarvam 4
103 Operating System Galvin Wiley 6
Modified content of list in backward is:
8 BPB Yashwant Kanetkar Let us C 101
4 TechSarvam Rajendra Java Program Question 102
6 Wiley Galvin Operating System 103
This program illustrates how to use ListIterator to traverse a List of objects in both forward and backward directions and how to modify the order in which object properties are printed.