• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Remove an element from array by index

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try to read this.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  •  
    Sheriff
    Posts: 22701
    129
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    Ranch Hand
    Posts: 441
    Scala IntelliJ IDE Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Scala IntelliJ IDE Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Posts: 22701
    129
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Scala IntelliJ IDE Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic