×

Java Programs

Java Practice

Java program to remove elements from specific index from an ArrayList

In this program, we are going to learn create an ArrayList, adding elements at specific index and print them on output screen. By IncludeHelp Last updated : December 31, 2023

Problem statement

Given an ArrayList and we have to remove some specific record from it in Java.

Removing elements from specific index from an ArrayList

To remove an element from ArrayList, use the ArrayList.remove() method and pass the specific index.

Java program to remove elements from specific index from an ArrayList

Here, we are creating an ArrayList and adding 5 elements in it (100, 200, 300, 400 and 500) and further, we are removing 2 elements from index 1 and 3.

import java.util.ArrayList;
 
public class ExArrayList {
 
  public static void main(String[] args) {
    ////Creating object of ArrayList
    ArrayList arrList = new ArrayList();
   
    //adding data to the list
    arrList.add("100");
    arrList.add("200");
    arrList.add("300");
    arrList.add("400");
    arrList.add("500");
   
    System.out.println("Array List elements: ");
    //display elements of ArrayList
    for(int iLoop=0; iLoop < arrList.size(); iLoop++)
      System.out.println(arrList.get(iLoop));
   
    //removing some of the elements
    //removing two elements from index 1 and 3
    arrList.remove(1);
    arrList.remove(3);
    
    
    System.out.println("Array List elements: ");
    //display elements of ArrayList after removing 
    for(int iLoop=0; iLoop < arrList.size(); iLoop++)
      System.out.println(arrList.get(iLoop));
  
  }
}

Output

The output of the above example is:

Array List elements: 
100
200
300
400
500
Array List elements: 
100
300
400

Java ArrayList Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.