Removing An Element From An Array In Java

Sharing is healthy !!

Hey guys!! This program will show you removing an element from an array in java. Below is java program,

Example: Removing An Element From An Array In Java

Also Read – Insertion Sort In Java

public class DeleteElementFromArray {

public static void main(String[] args) {

System.out.println("Before removing or deleting an element from an array:");
int[] example = {2,4,6,8,10};
int position = 5;

for(int x = 0;x < example.length;x++){
System.out.println(example[x]);
}

for(int i = 0;i < example.length;i++){
if(position == i){
for(int y = i + 1;i < example.length - 1;y++){
example[i] = example[y];
}
}
}

System.out.println("After removing or deleting an element from an array:");
for(int a = 0;a < example.length - 1;a++){
System.out.println(example[a]);
}
}
}

Output:

Removing An Element From An Array In Java

Sharing is healthy !!

Leave a Reply

Your email address will not be published.