How to exclude specific content from 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 :