Tutorials

Java Collection Programs


Arraylist update item


In this chapter, our task is,

  • create an arraylist
  • add elements to arraylist
  • update the arraylist (remove element from arraylist)

See the code snippet given below:


package tihArrayList;

import java.util.ArrayList;
import java.util.Iterator;

public class UpdateList {

      public static void main(String[] args) {

          //create Array List
          ArrayList tihList = new ArrayList();

          //Add elements to ArrayList
          tihList.add("Java");
          tihList.add("Spring");
          tihList.add("Hibernate");
          tihList.add("EJB");

          System.out.println("Original list:" +tihList);                           

          Iterator itr = tihList.iterator();

          while(itr.hasNext()){

                String token = itr.next();

                itr.remove();     //removing element from arraylist

                System.out.println("list afer modification:" +tihList);

          }                                            

    }

}

OUTPUT

Original list:[Java, Spring, Hibernate, EJB]
list afer modification:[Spring, Hibernate, EJB]
list afer modification:[Hibernate, EJB]
list afer modification:[EJB]
list afer modification:[]

 

 



Please Share this page
Views : 69
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!