Delete slot in the middle of an array and shift remaining

Discussion in 'Java' started by Sven Fischer, Nov 25, 2011.

  1. Sven Fischer

    Sven Fischer Guest

    Assume I have an array of strings like

    String[] slot = new String[];

    slot[0] = new String(first");
    slot[1] = new String(second");
    slot[2] = new String(third");
    slot[3] = new String(foo");
    slot[4] = new String("bar");

    Now I want to delete e.g. the third slot and shift all remaining slots 1 slot upwards.

    The result should look like:

    slot[0] = new String(first");
    slot[1] = new String(second");
    slot[2] = new String(foo");
    slot[3] = new String("bar");

    How can I do this most easily?
    Is this possible without iteration in a "while" loop?

    Sven
    Sven Fischer, Nov 25, 2011
    #1
    1. Advertising

  2. Sven Fischer

    Lew Guest

    Sven Fischer wrote:
    > Assume I have an array of strings like
    >
    > String[] slot = new String[];
    >
    > slot[0] = new String(first");


    You should post complete, compilable code in the future. See http://sscce.org/

    Here it would have helped you not omit the quotation mark.

    > slot[1] = new String(second");


    If "second" already is a 'String', why do you want to make a new 'String' from it?

    > slot[2] = new String(third");
    > slot[3] = new String(foo");
    > slot[4] = new String("bar");
    >
    > Now I want to delete e.g. the third slot and shift all remaining slots 1 slot upwards.
    >
    > The result should look like:
    >
    > slot[0] = new String(first");
    > slot[1] = new String(second");
    > slot[2] = new String(foo");
    > slot[3] = new String("bar");
    >
    > How can I do this most easily?


    Use a loop, such as 'while', 'do'...'while' or 'for'.

    > Is this possible without iteration in a "while" loop?


    Yes.

    --
    Lew
    Lew, Nov 25, 2011
    #2
    1. Advertising

  3. Sven Fischer

    Eric Sosman Guest

    On 11/25/2011 5:39 AM, Sven Fischer wrote:
    > Assume I have an array of strings like
    >
    > String[] slot = new String[];


    Won't compile as written; let's assume "new String[5]".

    > slot[0] = new String(first");


    More uncompilable stuff. I'm just going to guess at what I
    think you meant and talk about my guesses; if they don't match up
    with your actual intent that's not my fault ...

    I'll mention in passing that "new String(aString)" is almost
    always silly, also wasteful.

    > slot[1] = new String(second");
    > slot[2] = new String(third");
    > slot[3] = new String(foo");
    > slot[4] = new String("bar");
    >
    > Now I want to delete e.g. the third slot and shift all remaining slots 1 slot upwards.
    >
    > The result should look like:
    >
    > slot[0] = new String(first");
    > slot[1] = new String(second");
    > slot[2] = new String(foo");
    > slot[3] = new String("bar");


    ... and now the "[5]" returns to haunt you, because there's no
    way to get rid of slot[4]. You can leave it still pointing to "bar"
    or you can set it equal to null or you can point it to "Black Friday",
    but slot[4] still exists and must have *some* kind of value.

    If you really want slot[4] to disappear altogether, your only
    recourse is to make a brand-new shorter array, copy the interesting
    bits, and then jettison the entire old array and use the new one
    in its place:

    String[] newslot = new String[4];
    newslot[0] = slot[0];
    newslot[1] = slot[1];
    newslot[2] = slot[3];
    newslot[3] = slot[4];
    slot = newslot; // out with the old, in with the new

    > How can I do this most easily?


    It depends on what you want "this" to do with slot[4]. Your
    basic alternatives are (1) shift the content of the existing array
    around and maybe do something with slot[4], or (2) copy the good
    bits into a brand-new array as I've shown.

    > Is this possible without iteration in a "while" loop?


    Sure. You could do it with a for loop, or a do loop, or by
    recursion, or by straight-line assignments, or with System.arrayCopy(),
    or by Arrays.sort() with a really weird Comparator, or ...

    If I, personally, were doing this, my first question would be "Is
    this operation necessary?" And I'd ponder whether using an array, as
    opposed to a List<String> (perhaps a LinkedList<String>) might let me
    avoid the whole delete-and-slide thing to begin with. If I decided that
    an array was actually required (for some reason not apparent in your
    example), then I'd ask whether it was important to preserve the original
    order among the survivors: Maybe "slot[2] = slot[4]; slot[4] = null;"
    would suffice. If that, too, turned out to be a non-starter, I'd most
    likely use System.arrayCopy() to slide the surviving tail downward.

    --
    Eric Sosman
    d
    Eric Sosman, Nov 25, 2011
    #3
  4. Sven Fischer

    markspace Guest

    On 11/25/2011 2:39 AM, Sven Fischer wrote:
    > Assume I have an array of strings like


    > How can I do this most easily?
    > Is this possible without iteration in a "while" loop?



    I love the smell of homework in the morning... smells like... parents
    wasting their money on their children's education.
    markspace, Nov 25, 2011
    #4
  5. On 11/25/2011 12:21 PM, Lew wrote:
    > Sven Fischer wrote:


    > You should post complete, compilable code in the future. See http://sscce.org/


    Why? He asked for an algorithm.

    Magnus
    Magnus Warker, Nov 25, 2011
    #5
  6. Sven Fischer

    Lew Guest

    Magnus Warker wrote:
    > Lew wrote:
    >> Sven Fischer wrote:
    >> You should post complete, compilable code in the future. See http://sscce.org/

    >
    > Why? He asked for an algorithm.


    He posted completely uncompilable code fragments, thus clearly evidencing that his problems were deeper-rooted than simple algorithm advice could help.. The grasp of fundamentals is a prerequisite for any advice to actually help. Preparing an SSCCE would force the OP to confront the manifold problems in their code, problems that would make their code useless no matter howgood the algorithm, and solve them early. This will benefit the OP far more than answering only the question they asked and leaving them hanging with their deeper problems.

    Tell you what, Magnus, why don't *you* navigate to the link and read it? It explains there the usefulness of the SSCCE. You could have saved yourself some time and gotten some good information while you were at it if you'd gone there first. It's really good, useful stuff, I promise, or I wouldn'thave shared it.

    You *do* want to help the OP, don't you?

    --
    Lew
    Lew, Nov 25, 2011
    #6
  7. Sven Fischer

    markspace Guest

    It's a bit cleaner this way.


    On 11/25/2011 10:17 AM, Patricia Shanahan wrote:
    >
    > public class ArrayDelete {
    >
    > public static void main(String[] args) {
    > String[] slot =


    {"first", "second", "third", "foo", "bar" };

    >
    > // Now I want to delete e.g. the third slot and shift all remaining
    > slots 1 slot upwards.
    > //
    > // The result should look like:
    > //
    > // slot[0] = new String(first");
    > // slot[1] = new String(second");
    > // slot[2] = new String(foo");
    > // slot[3] = new String("bar");
    > }
    >
    > }
    >
    markspace, Nov 25, 2011
    #7
  8. Sven Fischer

    Lew Guest

    Patricia Shanahan wrote:
    > Magnus Warker wrote:
    >> Lew wrote:
    >>> Sven Fischer wrote:
    > >> You should post complete, compilable code in the future. See
    > >> http://sscce.org/

    > >
    > > Why? He asked for an algorithm.

    >
    > I agree. However, for anyone who feels really strongly that they need an
    > SSCCE to even look at a question, here is one based on the original article:


    For the record, as I explained earlier I did not suggest an SSCCE because I feel ("really strongly" or otherwise) that I "need an SSCCE even to look at a question", but for pedagogical purposes specific to the OP's situation.

    The value of the SSCCE isn't achieved by your providing it, but by the OP going through the exercise.

    Thanks for the good example, but I hope the OP doesn't just skip the work because they think your answer is enough for them.

    It isn't.

    --
    Lew
    Lew, Nov 25, 2011
    #8
  9. Sven Fischer

    Roedy Green Guest

    On 25 Nov 2011 10:39:52 GMT, (Sven Fischer)
    wrote, quoted or indirectly quoted someone who said :

    >How can I do this most easily?
    >Is this possible without iteration in a "while" loop?


    The easy and standard way to do this is to use an ArrayList. It has a
    method to do this. Arrays are for groups of elements where you do not
    add or remove after initial creation.
    see http://mindprod.com/jgloss/arraylist.html

    A slightly more hairy-chested way to do it is to use System.arraycopy
    which is a native method that can take advantage of the hardware's
    ability to move whacking hunks of bytes around at a time.
    see http://mindprod.com/jgloss/array.html

    To do it the Pascalan way you need a loop that maintains two indexes
    from and to. They both increment/decrement each time around the loop.
    If there is overlap, you must work either up or down to avoid
    overwriting. see http://mindprod.com/jgloss/jcheat.html for hints on
    how to do loops with two indexes.

    You can peek inside the ArrayList code to see how it handles inserting
    and deleting. look inside src.zip.

    This takes me back. I wrote various moves that implemented this logic
    for BBL Forth/Abundance.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    I can't come to bed just yet. Somebody is wrong on the Internet.
    Roedy Green, Nov 25, 2011
    #9
  10. Sven Fischer

    Roedy Green Guest

    On Fri, 25 Nov 2011 10:17:50 -0800, Patricia Shanahan <>
    wrote, quoted or indirectly quoted someone who said :

    > slot[0] = new String("first");


    that "new String" is what I term "newbie stuttering". You can write
    it as
    slot[ 0 ] = "first";

    "first" is already a String.

    Patricia was just tidying to create an SSCCE. She elected not to tidy
    that up.

    see http://mindprod.com/jgloss/stuttering.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    I can't come to bed just yet. Somebody is wrong on the Internet.
    Roedy Green, Nov 25, 2011
    #10
  11. Sven Fischer

    Arne Vajhøj Guest

    On 11/25/2011 5:39 AM, Sven Fischer wrote:
    > Assume I have an array of strings like
    >
    > String[] slot = new String[];
    >
    > slot[0] = new String(first");
    > slot[1] = new String(second");
    > slot[2] = new String(third");
    > slot[3] = new String(foo");
    > slot[4] = new String("bar");
    >
    > Now I want to delete e.g. the third slot and shift all remaining slots 1 slot upwards.
    >
    > The result should look like:
    >
    > slot[0] = new String(first");
    > slot[1] = new String(second");
    > slot[2] = new String(foo");
    > slot[3] = new String("bar");
    >
    > How can I do this most easily?


    Switch from String[] to LinkedList<String>.

    > Is this possible without iteration in a "while" loop?


    If you want to keep using String[], then you need to
    allocate a new array and copy the elements to be kept.
    You can use a for loop or System.arraycopy.

    Arne
    Arne Vajhøj, Nov 26, 2011
    #11
  12. Sven Fischer

    Arne Vajhøj Guest

    On 11/25/2011 2:26 PM, Lew wrote:
    > Patricia Shanahan wrote:
    >> Magnus Warker wrote:
    >>> Lew wrote:
    >>>> Sven Fischer wrote:
    >>>> You should post complete, compilable code in the future. See
    >>>> http://sscce.org/
    >>>
    >>> Why? He asked for an algorithm.

    >>
    >> I agree. However, for anyone who feels really strongly that they need an
    >> SSCCE to even look at a question, here is one based on the original article:

    >
    > For the record, as I explained earlier I did not suggest an SSCCE because I feel ("really strongly" or otherwise) that I "need an SSCCE even to look at a question", but for pedagogical purposes specific to the OP's situation.
    >
    > The value of the SSCCE isn't achieved by your providing it, but by the OP going through the exercise.


    In this case there is no value of OP providing compilable code.

    Arne
    Arne Vajhøj, Nov 26, 2011
    #12
  13. Sven Fischer

    Arne Vajhøj Guest

    On 11/25/2011 12:55 PM, Lew wrote:
    > Magnus Warker wrote:
    >> Lew wrote:
    >>> Sven Fischer wrote:
    >>> You should post complete, compilable code in the future. See http://sscce.org/

    >>
    >> Why? He asked for an algorithm.

    >
    > He posted completely uncompilable code fragments, thus clearly
    > evidencing that his problems were deeper-rooted than simple algorithm
    > advice could help. The grasp of fundamentals is a prerequisite for any
    > advice to actually help. Preparing an SSCCE would force the OP to
    > confront the manifold problems in their code, problems that would make
    > their code useless no matter how good the algorithm, and solve them
    > early. This will benefit the OP far more than answering only the
    > question they asked and leaving them hanging with their deeper problems.


    There are no indications of OP having any fundamental problems.

    He posted a clear question.

    If you want to answer it, then feel free to do so.

    If you by principle don't want to answer questions if the post
    contains uncompilable code, then feel free to no answer it.

    But please don't spam the group with SSCCE replies, if the question
    is clear (or should be for any programmer).

    > Tell you what, Magnus, why don't *you* navigate to the link and read
    > it? It explains there the usefulness of the SSCCE. You could have saved
    > yourself some time and gotten some good information while you were at it
    > if you'd gone there first. It's really good, useful stuff, I promise, or
    > I wouldn't have shared it.


    Why should he?

    He seems to be able to understand the question asked.

    Arne
    Arne Vajhøj, Nov 26, 2011
    #13
  14. Sven Fischer

    Sven Fischer Guest

    Thank you Arne. This is my opinion as well.

    I have sometimes a problem which is in reality a complex problem and a
    code of many ,many lines.
    In order to offer users only the core problem I simplify heavily the
    code.

    So mistake could occur.

    Accordingly I expect as solution no runnable code but an idea.
    This is sufficient.
    Thank you for your other suggestions.
    Sven

    On Fri, 25 Nov 2011 21:22:37 -0500, Arne Vajhøj <>
    wrote:

    >On 11/25/2011 12:55 PM, Lew wrote:
    >> Magnus Warker wrote:
    >>> Lew wrote:
    >>>> Sven Fischer wrote:
    >>>> You should post complete, compilable code in the future. See http://sscce.org/
    >>>
    >>> Why? He asked for an algorithm.

    >>
    >> He posted completely uncompilable code fragments, thus clearly
    >> evidencing that his problems were deeper-rooted than simple algorithm
    >> advice could help. The grasp of fundamentals is a prerequisite for any
    >> advice to actually help. Preparing an SSCCE would force the OP to
    >> confront the manifold problems in their code, problems that would make
    >> their code useless no matter how good the algorithm, and solve them
    >> early. This will benefit the OP far more than answering only the
    >> question they asked and leaving them hanging with their deeper problems.

    >
    >There are no indications of OP having any fundamental problems.
    >
    >He posted a clear question.
    >
    >If you want to answer it, then feel free to do so.
    >
    >If you by principle don't want to answer questions if the post
    >contains uncompilable code, then feel free to no answer it.
    >
    >But please don't spam the group with SSCCE replies, if the question
    >is clear (or should be for any programmer).
    >
    >> Tell you what, Magnus, why don't *you* navigate to the link and read
    >> it? It explains there the usefulness of the SSCCE. You could have saved
    >> yourself some time and gotten some good information while you were at it
    >> if you'd gone there first. It's really good, useful stuff, I promise, or
    >> I wouldn't have shared it.

    >
    >Why should he?
    >
    >He seems to be able to understand the question asked.
    >
    >Arne
    Sven Fischer, Nov 26, 2011
    #14
  15. On Sat, 26 Nov 2011 13:48:17 +0100, Sven Fischer
    <> wrote:

    >Thank you Arne. This is my opinion as well.


    It is also mine.

    >I have sometimes a problem which is in reality a complex problem and a
    >code of many ,many lines.
    >In order to offer users only the core problem I simplify heavily the
    >code.
    >
    >So mistake could occur.


    And if I do it, I will probably be discussing algorithms anyway,
    so I really do not care about the implementation details at that
    point.

    >Accordingly I expect as solution no runnable code but an idea.
    >This is sufficient.
    >Thank you for your other suggestions.


    [snip]

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Nov 27, 2011
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto Gallo

    Shift - byte[] buf shift

    Roberto Gallo, Jan 27, 2004, in forum: Java
    Replies:
    3
    Views:
    2,045
    Thomas Schodt
    Jan 27, 2004
  2. Wenjie
    Replies:
    3
    Views:
    1,031
    Ron Samuel Klatchko
    Jul 11, 2003
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,446
    CBFalconer
    Nov 30, 2006
  4. Sanny
    Replies:
    38
    Views:
    3,380
    Thomas Richter
    Apr 29, 2011
  5. devphylosoff

    what "shift" does, if not "$_ = shift;" ?

    devphylosoff, Nov 29, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    325
    Michele Dondi
    Dec 4, 2007
Loading...

Share This Page