Java ListIterator iterate and modify element

Introduction

To iterate and modify element via ListIterator in Java

//get ListIterator from List
ListIterator<String> litr = al.listIterator();  

// Modify objects being iterated. 
while(litr.hasNext()) { 
      String element = litr.next(); 
      litr.set(element + "+"); 
} 

Full source

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator; 
 
public class Main { 
  public static void main(String args[]) { 
    // Create an array list. 
    List<String> al = new ArrayList<String>(); 
     //ww  w. java  2s .c o m
    // Add elements to the array list. 
    al.add("SQL"); 
    al.add("Java"); 
    al.add("Javascript"); 
    al.add("CSS"); 
    al.add("HTML"); 
    al.add("Demo2s.com"); 
    al.add(1, "Hi"); 
 
    System.out.println(al);
    
    // Modify objects being iterated. 
    ListIterator<String> litr = al.listIterator();  
    while(litr.hasNext()) { 
      String element = litr.next(); 
      litr.set(element + "+"); 
    } 
    System.out.println(al);
  } 
}



PreviousNext

Related