The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Example Program to Remove element from specified index ArrayList

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java Example Program to Remove element from specified index ArrayList Posted: Mar 9, 2016 7:41 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Example Program to Remove element from specified index ArrayList
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
 1.Basic java Example program to remove an element from specified index ArrayList.

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  
  4. public class RemoveElementArrayList{
  5.  
  6. public static void main(String[] args) {
  7.  
  8.    //create an ArrayList object
  9.         ArrayList<String> arrayList = new ArrayList<String>();
  10.        
  11.         //Add elements to Arraylist
  12.         arrayList.add("A");
  13.         arrayList.add("B");
  14.         arrayList.add("C"); 
  15.         arrayList.add("A");
  16.        
  17.   /*
  18.    To remove an element from the specified index of ArrayList use
  19.     Object remove(int index) method.
  20.     It returns the element that was removed from the ArrayList. */
  21.  
  22.     Object obj = arrayList.remove(1);
  23.      System.out.println(obj + " is removed from ArrayList");
  24.        
  25.   System.out.println("ArrayList contains...");
  26.   //display ArrayList elements using for loop
  27.     for(int index=0; index < arrayList.size(); index++)
  28.     System.out.println(arrayList.get(index));
  29.   
  30.  
  31.  }
  32. }
     


Output:

  1. B is removed from ArrayList
  2. ArrayList contains...
  3. A
  4. C
  5. A

     
 2.Basic java Example program to remove all elements from ArrayList.

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  
  4. public class RemoveAllElementArrayList{
  5.  
  6. public static void main(String[] args) {
  7.  
  8.    //create an ArrayList object
  9.         ArrayList<String> arrayList = new ArrayList<String>();
  10.        
  11.         //Add elements to Arraylist
  12.         arrayList.add("A");
  13.         arrayList.add("B");
  14.         arrayList.add("C"); 
  15.         arrayList.add("A");
  16.        
  17.  
  18. System.out.println("Size of ArrayList before removing elements : "+ arrayList.size());
  19.  
  20. /*
  21. To remove all elements from the ArrayList we need to use
  22. void clear() method.
  23. */
  24. arrayList.clear();
  25. System.out.println("Size of ArrayList after removing elements : "+ arrayList.size());  
  26.  
  27.  }
  28. }
     
Output:

     
  1. B is removed from ArrayList
  2. ArrayList contains...
  3. A
  4. C
  5. A

Read: Java Example Program to Remove element from specified index ArrayList

Topic: Kids : HouseHold Items v6 Previous Topic   Next Topic Topic: Docker and Jenkins – Orchestrating Continuous Delivery

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use