Tutorials Point


  Java Basics Examples
  Java Tutorial
  Java Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Java Examples - Remove an element


previous next AddThis Social Bookmark Button


Advertisements

Problem Description:

How to remove an element of array?

Solution:

Following example shows how to remove an element from array.

import java.util.ArrayList;

public class Main {
   public static void main(String[] args)  {
      ArrayList objArray = new ArrayList();
      objArray.clear();
      objArray.add(0,"0th element");
      objArray.add(1,"1st element");
      objArray.add(2,"2nd element");
      System.out.println("Array before removing an element"+objArray);
      objArray.remove(1);
      objArray.remove("0th element");
      System.out.println("Array after removing an element"+objArray);
   }
}

Result:

The above code sample will produce the following result.

Array before removing an  element[0th element, 1st element, 2nd element]
Array after removing an element[2nd element]


previous next Printer Friendly

Advertisements


  

Advertisements