-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
  • 55,379
  • 16
  • 141
  • 208
goku
  • 223
  • 2
  • 12

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
  • 72,141
  • 5
  • 83
  • 121
0

You can use it :

array = ArrayUtils.removeElement(array, element)

Documentation

OlivierH
  • 3,875
  • 1
  • 19
  • 32
0

Use ArrayUtils class to remove an element

 array = ArrayUtils.removeElement(array,element)
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 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
  • 15,208
  • 4
  • 51
  • 78