Share & learn

Through Innovative Digital Library

How to exclude specific content from array in Java?

How to exclude specific content from an array in Java?


Posted by Paul on April 30th 2018 13:02 PM

Excluding particular element from an array : There is no method in java as remove() to exclude particular element from an array. The reason behind this is that Arrays are of fixed size in Java, which means, once an array is created you can't change its size. Which means removing or deleting an item doesn't reduce the size of the array. To delete element from a particular index, you need to shift the elements having higher index to left by one position. e.g. Consider that you want to remove element at index 4, so you have to shift element at index 4 to 3, element at index 5 to element at index 4, element at index 6 to element at index 5 and so on. Note : This is just shifting of elements and it looks like removing the element. But if you check the array length after everything is done, it will be same. Here is the code :

// Java program to exclude an element from an array
public class ExcludeElementArray {

    public static 
void main(String[] args) {
// TODO Auto-generated method stub
        // array of int type
int[] arr = new int[] { 50100150200250 };

// index of element to be removed
int remIndex 3;

// check length of array before deletion
System.out.println("Array Size Before Deletion : " arr.length);
System.out.println("Array Before Deletion : " );

// print array before deletion
for (int i 0arr.lengthi++) {

System.out.print(arr[i] + " ");


// remove element at index 3
for (int i remIndexarr.length 1i++) {

arr[i] = arr[1];


// print array after deletion
System.out.println("\n\nArray After Deletion");

        for (
int i 0arr.length 1i++) {

System.out.print(arr[i] + " ");


// check length of array before deletion
System.out.println("\nArray Size After Deletion : " arr.length);


Replied by abhishek on July 4th 2018 17:59 PM

fg dfgsd f g sdg sd f

Replied by abhishek on July 4th 2018 18:04 PM

das asd fa sd fa sdfas d fa sd fasd 

Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character

related questions

Most liked questions

Most answered questions

R3RL Refresh
R3RL Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Minimum 20 character
R3RL Refresh