-3

I want to delete the selected element in an array in java, for example deleting 3,5,8 in

int[]={3,2,5,6,4,8,9}
masoud
  • 53,199
  • 16
  • 130
  • 198
goku
  • 223
  • 2
  • 11

4 Answers4

1

You can't. Arrays have a fixed length, and the only way to "delete" them would be to either use for example zero to mark a "deleted" entry, or to create a new smaller array and copy all but the "deleted" entries there.

Kayaman
  • 70,361
  • 5
  • 75
  • 119
0

You can use it :

array = ArrayUtils.removeElement(array, element)

Documentation

OlivierH
  • 3,817
  • 1
  • 18
  • 30
0

Use ArrayUtils class to remove an element

 array = ArrayUtils.removeElement(array,element)
Kamlesh Arya
  • 4,644
  • 2
  • 18
  • 28
0

Arrays cannot be resized, so you either create a new array and copy the desired elements from the old one (use System.arraycopy(..)) for that, or better: use a Collection such as ArrayList or LinkedList.

Peter Walser
  • 14,278
  • 4
  • 52
  • 73