This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Remove an element from array by index Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Remove an element from array by index " Watch "Remove an element from array by index " New topic

Remove an element from array by index

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278

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.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

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

Joined: Feb 17, 2011
Posts: 21

try to read this.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43975
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

    Joined: Oct 27, 2005
    Posts: 20078

    If you need to remove elements, an array isn't a good choice. Can't you use a List instead?

    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
    How To Ask Questions How To Answer Questions
    Luigi Plinge
    Ranch Hand

    Joined: Jan 06, 2011
    Posts: 441

    If you need to do this a lot and performance is important, a LinkedList might be the best option.
    Luigi Plinge
    Ranch Hand

    Joined: Jan 06, 2011
    Posts: 441

    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

    Joined: Oct 27, 2005
    Posts: 20078

    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

    Joined: Jan 06, 2011
    Posts: 441

    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.
    Have you checked out Aspose?
    subject: Remove an element from array by index
    jQuery in Action, 3rd edition