Found 9313 Articles for Object Oriented Programming

Use ListIterator to traverse an ArrayList in the forward direction in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

165 Views

A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in the List Collection. So the ListIterator is only valid for classes such as LinkedList, ArrayList etc.The method hasNext( ) in ListIterator returns true if there are more elements in the List and false otherwise. The method next( ) returns the next element in the List and advances the cursor position.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = ... Read More

Iterate through a Collection using an Iterator in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

187 Views

A collection in Java provides an architecture to handle a group of objects. The different classes in the Java Collection Framework are ArrayList, LinkedList, HashSet, Vector etc.An Iterator can be used to iterate through a Collection and a program that demonstrates this using ArrayList is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("John");       aList.add("Peter");       aList.add("Harry");       aList.add("James");       aList.add("Arthur");       System.out.println("The ArrayList elements are: "); ... Read More

Iterate through a LinkedList using an Iterator in Java

Chandu yadav
Updated on 29-Jun-2020 13:53:57

6K+ Views

An Iterator can be used to loop through an LinkedList. The method hasNext( ) returns true if there are more elements in LinkedList and false otherwise. The method next( ) returns the next element in the LinkedList and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("John");       l.add("Sara");       l.add("Susan");       l.add("Betty");       l.add("Nathan");   ... Read More

Use Iterator to remove an element from a Collection in Java

Arjun Thakur
Updated on 29-Jun-2020 13:54:59

17K+ Views

An element can be removed from a Collection using the Iterator method remove(). This method removes the current element in the Collection. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Mango");       aList.add("Guava");       aList.add("Orange");       aList.add("Peach");       System.out.println("The ArrayList elements are: ");     ... Read More

Loop through a HashMap using an Iterator in Java

Ankith Reddy
Updated on 29-Jun-2020 13:55:50

175 Views

An Iterator can be used to loop through a HashMap. The method hasNext( ) returns true if there are more elements in HashMap and false otherwise. The method next( ) returns the next key element in the HashMap and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Demo {    public static void main(String[] args) {       Map student = new HashMap();       student.put("101", "Harry");       student.put("102", "Amy");       student.put("103", "John");       ... Read More

Loop through an ArrayList using an Iterator in Java

George John
Updated on 29-Jun-2020 13:56:42

4K+ Views

An Iterator can be used to loop through an ArrayList. The method hasNext( ) returns true if there are more elements in ArrayList and false otherwise. The method next( ) returns the next element in the ArrayList and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Mango");       aList.add("Guava");       aList.add("Orange");       aList.add("Peach");   ... Read More

Create an object array from elements of LinkedList in Java

Chandu yadav
Updated on 29-Jun-2020 13:45:11

340 Views

An object array can be created from the elements of a LinkedList using the method java.util.LinkedList.toArray(). This method returns the object array with all the LinkedList elements in the correct order.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Amy");       l.add("Sara");       l.add("Joe");       l.add("Betty");       l.add("Nathan");       Object[] objArr = l.toArray();       System.out.println("The object array elements are: ");       for ... Read More

Iterate through elements of a LinkedList using a ListIterator in Java

Ankith Reddy
Updated on 29-Jun-2020 13:46:41

183 Views

A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in a LinkedList.The method hasNext( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the forward direction and false otherwise. The method next( ) returns the next element in the LinkedList and advances the cursor position.The method hasPrevious( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the reverse direction and false otherwise. The method previous( ) returns the previous element in the LinkedList and reduces the ... Read More

Replace an element in an ArrayList using the ListIterator in Java

George John
Updated on 29-Jun-2020 13:47:32

1K+ Views

An element in ArrayList can be replaced using the ListIterator method set(). This method has a single parameter i.e. the element that is to be replaced and the set() method replaces it with the last element returned by the next() or previous() methods.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Amanda");       aList.add("Taylor");       aList.add("Justin");       aList.add("Emma");       aList.add("Peter");       System.out.println("The ArrayList elements ... Read More

Remove an element from an ArrayList using the ListIterator in Java

Chandu yadav
Updated on 29-Jun-2020 13:48:11

796 Views

An element can be removed from an ArrayList using the ListIterator method remove(). This method removes the current element in the ArrayList. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Mango");       aList.add("Guava");       aList.add("Orange");       aList.add("Peach");       System.out.println("The ArrayList elements are: ");     ... Read More

Advertisements