Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove an element from array by index

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi all ,

Do we have any method in Java in String or StringUtils to remove an element from a string aray by index.
like remove(startindex,endIndex) in C#.
I looked over internet and StringUtils too,but i couldnt find any.

What would be the simplest method without adding performance -slow-down to my project to do this?
Using StringBuffer?
I already have lot of Stringbuilder -String conversions in my method.

Thanks
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a StringBuffer or StringBuilder doesn't model an array of Strings, so that really isn't a candidate anyway. What I would recommend is to stop using arrays if you need a list of strings whose length can change from time to time. In particular I would recommend an ArrayList<String> -- if you check the documentation you'll find it has a method to remove a sub-list as you described.
 
Death Arte
Greenhorn
Posts: 21
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to read this.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The alternative would be to
  • Create an array one size smaller
  • Copy all the items from 0 to n - 2 into the new array
  • Copy all the items from n onwards into the new array
  • Assign the new array to the reference of the old array.
  •  
    Rob Spoor
    Sheriff
    Pie
    Posts: 20605
    60
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you need to remove elements, an array isn't a good choice. Can't you use a List instead?
     
    Luigi Plinge
    Ranch Hand
    Posts: 441
    IntelliJ IDE Scala Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you need to do this a lot and performance is important, a LinkedList might be the best option.
     
    Luigi Plinge
    Ranch Hand
    Posts: 441
    IntelliJ IDE Scala Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually, scrub that - I just did a quick test and LinkedLists are slow...

    LinkedList = 136,060,386
    ArrayList = 19,373,657
    LinkedList = 89,518,985
    ArrayList = 18,704,275


     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20605
    60
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    LinkedLists are great when:
    - adding to the start or the end
    - removing from the start or the end
    - adding, removing or setting using a ListIterator

    All direct element accesses will require the list to be traversed until that element is found, and that's what makes it slow if you use any indexed method.
     
    Luigi Plinge
    Ranch Hand
    Posts: 441
    IntelliJ IDE Scala Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Taking out the "remove" and instead adding an element at end:
    LinkedList = 8,998,243
    ArrayList = 6,915,845
    LinkedList = 9,271,848
    ArrayList = 6,724,540


    Removing last element:
    LinkedList = 9,612,391
    ArrayList = 7,236,635
    LinkedList = 12,784,090
    ArrayList = 7,692,767


    Removing first element:
    LinkedList = 9,667,259
    ArrayList = 25,206,786
    LinkedList = 11,939,864
    ArrayList = 23,512,117
    (don't know why LinkedList is faster on first pass - this is consistent.

    Inserting first element:
    LinkedList = 9,510,338
    ArrayList = 36,440,690
    LinkedList = 9,834,421
    ArrayList = 36,002,118


    So better than ArrayLists for inserting / deleting at the start, but worse at the end.

    I don't know how to test ListIterators.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic