Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java list iterator example

// Java program to Iterate the Elements
// in an ArrayList

// Importing java utility classes
import java.util.*;

// Main class
public class Main {

	// main driver method
	public static void main(String args[])
	{
		// Creating an empty Arraylist of string type
		List<String> list = new ArrayList<>();

		// Adding elements to above object of ArrayList
		list.add("Mango");
		list.add("Banana");

		// Adding element at specified position
		// inside list object
		list.add(1, "Apple");

		// Using for loop for iteration
		for (int i = 0; i < list.size(); i++) {

			// Using get() method to
			// access particular element
			System.out.print(list.get(i) + " ");
		}

		// New line for better readability
		System.out.println();

		// Using for-each loop for iteration
		for (String str : list)

			// Printing all the elements
			// which was inside object
			System.out.print(str + " ");
	}
}
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #java #list #iterator
ADD COMMENT
Topic
Name
7+5 =