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

Package Import Public Class Public Static Void: "A" "D" "V" "C" "B" "J"

The document demonstrates how to use iterators and list iterators to traverse and modify elements in an ArrayList of Strings in Java. It creates an ArrayList, adds Strings, prints the original elements using an iterator. It then modifies the elements using a list iterator by appending "+" to each element. It obtains the iterator again and prints the modified elements both forward and backward using the list iterator.

Uploaded by

Hari Haran
Copyright
© Attribution Non-Commercial (BY-NC)
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)
68 views5 pages

Package Import Public Class Public Static Void: "A" "D" "V" "C" "B" "J"

The document demonstrates how to use iterators and list iterators to traverse and modify elements in an ArrayList of Strings in Java. It creates an ArrayList, adds Strings, prints the original elements using an iterator. It then modifies the elements using a list iterator by appending "+" to each element. It obtains the iterator again and prints the modified elements both forward and backward using the list iterator.

Uploaded by

Hari Haran
Copyright
© Attribution Non-Commercial (BY-NC)
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

package collection.view; import java.util.

*; public class IteratorDemo { public static void main(String[] args) { // CREATING AN ARRAYLIST ArrayList<String> al=new ArrayList<String>(); // STORING ELEMENTS IN ARRAYLIST al.add("A"); al.add("D"); al.add("V"); al.add("C"); al.add("B"); al.add("J"); // PRINTING THE ELEMENTS VIA ITERATOR System.out.print("Original Element:"); Iterator<String> itr=al.iterator(); while(itr.hasNext()) { String element=itr.next(); System.out.print(element+" "); } System.out.println(); // MODIFYING THE ELEMENTS OF THE ARRAYLIST USING LISTITERATOR ListIterator<String> litr=al.listIterator(); System.out.println("Modifying the collection elements:"); while(litr.hasNext()) { String element =litr.next(); litr.set(element+"+"); } // OBTAINING THE ITERATOR AGAIN litr=al.listIterator(); // PRINTING THE MODIFIED ELEMENTS USING ITERATOR System.out.print("Modified elements:"); while(litr.hasNext()) { String element=litr.next(); System.out.print(element); } System.out.println(); // PRINTING THE MODIFIED ELEMENTS USING LISTITERATOR

System.out.print("Modified element backward:"); while(litr.hasPrevious()) { String element=litr.previous(); System.out.print(element); } } }

package collection.domain; public class { public public String Student implements Comparable<Student> int id; String name; dept;

public Student(int id,String name,String dept) { this.id=id; this.name=name; this.dept=dept; }

public String toString() { return "Student [id=" + id + ", name=" + name + ", course=" + dept+ "]\n"; } public int compareTo(Student s) { int res=0; if(this.id<s.id) { res=-1; } else if(this.id>id) { res=1; } return res; }

package collection.dao; import java.util.*;

import collection.domain.Student; public class StudentStore { ArrayList<Student> al=new ArrayList<Student>();

public void register(Student s) { al.add(s);

} public void view() { System.out.println(al); }

public void remove(int id) { Iterator<Student> itr=al.iterator();

while(itr.hasNext())

{ Student element= itr.next(); if(id==element.id) { itr.remove(); //al.remove(element); System.out.println(al); }

} public void sort() { Collections.sort(al); System.out.println(al); } public void sort(Comparator<Student> ao) { Collections.sort(al,ao); System.out.println(al); }

import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class MyCollectionIterator { public static void main(String a[]){ List<String> myList = new ArrayList<String>(); myList.add("Java"); myList.add("Unix"); myList.add("Oracle"); myList.add("C++"); myList.add("Perl"); Iterator<String> itr = myList.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }

You might also like