0

How to delete an element from an array?

9th Dec 2016, 11:15 PM
chaitanya
chaitanya - avatar
4 Réponses
+ 1
Basic Java arrays are fixed length, you cannot remove elements. Try using an ArrayList instead.
9th Dec 2016, 11:23 PM
Kerrash
Kerrash - avatar
+ 1
As John indicates basic arrays can't be resized. This answer describes a function you can use to create a new array without the element, and also indicates ArrayList: http://stackoverflow.com/a/4870214/3981745
9th Dec 2016, 11:32 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
I skipped ArrayUtils because you're importing an entire library to do something better done directly. http://stackoverflow.com/a/4870274/3981745 "There are alternatives in the form of 3rd-party libraries (e.g. Apache Commons ArrayUtils), but you may want to consider whether it is worth adding a library dependency just for the sake of a method that you could implement yourself with 5-10 lines of code." ... or just use a list.
10th Dec 2016, 8:02 AM
Kirk Schafer
Kirk Schafer - avatar
0
array = ArrayUtils.removeElement(array, element)
10th Dec 2016, 2:49 AM
Somnath Ghosh
Somnath Ghosh - avatar