• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to delete an index.  RSS feed

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I want to delete an index of a string array. example String[] a=new String[5]. I want to remove the third index i.e i want to remove a[3]. Is this possible if so how please help me out.

Thanks and Regards
alexander
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot directly delete the object, but you can simply reassign the reference a[3] to null.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array in Java has a fixed length once it is created. Suppose that you have an array with 5 elements, then you cannot delete the fourth element and end up with an array with 4 elements.

If you need something more flexible than an array, use ArrayList instead of an array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!