0% found this document useful (0 votes)
4 views5 pages

Java Assignment

The document contains a Java assignment by Nikhil Kumar that includes various examples of using iterators and list iterators with ArrayLists. It demonstrates basic iteration, element removal, modification of elements, reverse iteration, and the use of spliterators to create a list of squares. Each example is accompanied by code snippets and outputs showcasing the results of the operations performed.

Uploaded by

xdnik76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Java Assignment

The document contains a Java assignment by Nikhil Kumar that includes various examples of using iterators and list iterators with ArrayLists. It demonstrates basic iteration, element removal, modification of elements, reverse iteration, and the use of spliterators to create a list of squares. Each example is accompanied by code snippets and outputs showcasing the results of the operations performed.

Uploaded by

xdnik76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA ASSIGNMENT

Name – Nikhil Kumar

Usn – 1CR23IS111

import java.util.*;

// Question 1: Basic Iteration with Iterator

public class IteratorExamples {

public static void main(String args[]) {

// Question 1

ArrayList<String> al1 = new ArrayList<>(Arrays.asList("Java", "Python", "C++", "JavaScript",


"Ruby"));

System.out.print("Contents of al1: ");

Iterator<String> itr1 = al1.iterator();

while(itr1.hasNext()) {

System.out.print(itr1.next() + " ");

System.out.println();

// Question 2: Remove Specific Element Using Iterator

ArrayList<Integer> al2 = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50));

Iterator<Integer> itr2 = al2.iterator();

while(itr2.hasNext()) {

if(itr2.next() == 30) {

itr2.remove();
}

System.out.println("Updated list after removal: " + al2);

// Question 3: Modify Elements Using ListIterator

ArrayList<String> al3 = new ArrayList<>(Arrays.asList("cat", "dog", "bird", "fish"));

ListIterator<String> litr3 = al3.listIterator();

while(litr3.hasNext()) {

litr3.set(litr3.next() + " animal");

System.out.println("Modified list: " + al3);

// Question 4: Reverse Iteration with ListIterator

ArrayList<String> al4 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry", "date"));

ListIterator<String> litr4 = al4.listIterator(al4.size());

System.out.print("Reverse order: ");

while(litr4.hasPrevious()) {

System.out.print(litr4.previous() + " ");

System.out.println("\nOriginal order: " + al4);

// Question 5: Search and Modify Using ListIterator

ArrayList<String> al5 = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));

ListIterator<String> litr5 = al5.listIterator();

while(litr5.hasNext()) {

if(litr5.next().equals("three")) {
litr5.set("three_modified");

System.out.println("Modified list: " + al5);

// Question 6: Remove All Elements Greater Than a Certain Value Using Iterator

ArrayList<Integer> al6 = new ArrayList<>(Arrays.asList(1, 5, 10, 15, 20, 25, 30));

Iterator<Integer> itr6 = al6.iterator();

while(itr6.hasNext()) {

if(itr6.next() > 15) {

itr6.remove();

System.out.println("Updated list: " + al6);

// Question 7: Add New Elements While Iterating Using ListIterator

ArrayList<Integer> al7 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

ListIterator<Integer> litr7 = al7.listIterator();

while(litr7.hasNext()) {

if(litr7.next() > 2) {

litr7.add(100);

System.out.println("Updated list: " + al7);

// Question 8: Check for Element and Modify Using Iterator


ArrayList<String> al8 = new ArrayList<>(Arrays.asList("cat", "dog", "elephant", "lion"));

ListIterator<String> litr8 = al8.listIterator();

while(litr8.hasNext()) {

if(litr8.next().equals("elephant")) {

litr8.set("tiger");

System.out.println("Updated list: " + al8);

// Question 9: Iterating and Removing Multiple Elements Using ListIterator

ArrayList<Integer> al9 = new ArrayList<>(Arrays.asList(10, 15, 20, 25, 30, 35, 40));

ListIterator<Integer> litr9 = al9.listIterator();

while(litr9.hasNext()) {

if(litr9.next() % 5 == 0) {

litr9.remove();

System.out.println("Updated list: " + al9);

// Question 10: Iterating Over a List and Replace Specific Elements

ArrayList<String> al10 = new ArrayList<>(Arrays.asList("red", "green", "blue", "yellow",


"purple"));

ListIterator<String> litr10 = al10.listIterator();

while(litr10.hasNext()) {

String element = litr10.next();

if(element.equals("green")) {
litr10.set("lime");

} else if(element.equals("blue")) {

litr10.set("sky blue");

System.out.println("Modified list: " + al10);

// Question 11: Spliterator to Iterate and Create Square List

ArrayList<Integer> al11 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

ArrayList<Integer> squares = new ArrayList<>();

Spliterator<Integer> spltitr = al11.spliterator();

spltitr.forEachRemaining(n -> squares.add(n * n));

System.out.println("List with squares: " + squares);

You might also like