Exercise:
Write a Java Program to iterate over an arraylist.
1.Iterate using for loop
Click Here to View the Solution!
import java.util.ArrayList;
public class IterateArraylist {
public static void main(String[] args) {
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("JavaScript");
languages.add("Python");
languages.add("CPP");
languages.add("C");
languages.add("Swift");
System.out.println("ArrayList: " + languages);
System.out.println("Iterating over ArrayList using for-loop: ");
for(int i = 0; i < languages.size(); i++) {
System.out.print(languages.get(i));
System.out.print(", ");
}
}
}
Click Here to View the Output!
ArrayList: [Java, JavaScript, Python, CPP, C, Swift] Iterating over ArrayList using for-loop: Java, JavaScript, Python, CPP, C, Swift,
Click Here to View the Explanation!
- In this example, a string type array list called
languages
is created firstly. - Elements are added to this array list and then the list is printed on the screen.
- Then the array list is iterated using a
for loop
and every element in the list is retrieved using theget() method
which is then printed on the screen.
2.Iterate using for-each loop
Click Here to View the Solution!
import java.util.ArrayList;
public class IterateArraylist {
public static void main(String[] args) {
// Creating an array list
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("JavaScript");
languages.add("Python");
languages.add("CPP");
languages.add("C");
languages.add("Swift");
System.out.println("ArrayList: " + languages);
System.out.println("Iterating over ArrayList using for-each loop:");
// Using for-each loop
for(String language : languages) {
System.out.print(language);
System.out.print(", ");
}
}
}
Click Here to View the Output!
ArrayList: [Java, JavaScript, Python, CPP, C, Swift] Iterating over ArrayList using for-each loop: Java, JavaScript, Python, CPP, C, Swift,
Click Here to View the Explanation!
- In this example, a string type array list called
languages
is created firstly. - Elements are added to this array list and then the list is printed on the screen.
- Then the array list is iterated using a
for-each loop
and every element in the listlanguages
is fetched and stored in the language variable which is then printed on the screen.
3.Iterate using listIterator()
Click Here to View the Solution!
import java.util.ArrayList;
import java.util.ListIterator;
public class IterateArraylist {
public static void main(String[] args) {
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("JavaScript");
languages.add("Python");
languages.add("CPP");
languages.add("C");
languages.add("Swift");
System.out.println("ArrayList: " + languages);
// Creating an instance of ListIterator
ListIterator<String> iterate = languages.listIterator();
System.out.println("Iterating over ArrayList:");
while(iterate.hasNext()) {
System.out.print(iterate.next() + ", ");
}
}
}
Click Here to View the Output!
ArrayList: [Java, JavaScript, Python, CPP, C, Swift] Iterating over ArrayList: Java, JavaScript, Python, CPP, C, Swift,
Click Here to View the Explanation!
- In this example, an integer type array list called
languages
is created firstly. - Elements are added to this array list and then the list is printed on the screen.
- Then an instance of
ListIterator
callediterate
is created. - A
while loop
is then executed which first checks if the next element exists in thearraylist
usinghasNext() method
. - If the condition is true, then the element in the
arraylist
is printed usingnext()
method. - This loop executes till it reaches the end of the array and so all elements of the list are printed