Found 9326 Articles for Object Oriented Programming

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

170 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

336 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

176 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

977 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

767 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

Obtain the Previous Index and Next Index in an ArrayList using the ListIterator in Java

Arjun Thakur
Updated on 29-Jun-2020 13:48:53

878 Views

The previous index and next index in an ArrayList can be obtained using the methods previousIndex() and nextIndex() respectively in the ListIterator Interface.The previousIndex() method returns the index of the element that is returned by the previous() method while the nextIndex() method returns the index of the element that is returned by the next() method. Neither of these methods require any parameters.A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Amy");       ... Read More

Iterate through a LinkedList in reverse direction using a ListIterator in Java

Ankith Reddy
Updated on 29-Jun-2020 13:49:56

737 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 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 cursor position backward.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       List l = new LinkedList();       ... Read More

Advertisements