Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10219 > unrolled thread

Delete slot in the middle of an array and shift remaining

Started bySvenfischer2@email.com (Sven Fischer)
First post2011-11-25 10:39 +0000
Last post2011-11-25 21:16 -0500
Articles 16 — 10 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Delete slot in the middle of an array and shift remaining Svenfischer2@email.com (Sven Fischer) - 2011-11-25 10:39 +0000
    Re: Delete slot in the middle of an array and shift remaining Lew <lewbloch@gmail.com> - 2011-11-25 03:21 -0800
      Re: Delete slot in the middle of an array and shift remaining Magnus Warker <magnux@mailinator.com> - 2011-11-25 18:44 +0100
        Re: Delete slot in the middle of an array and shift remaining Lew <lewbloch@gmail.com> - 2011-11-25 09:55 -0800
          Re: Delete slot in the middle of an array and shift remaining Arne Vajhøj <arne@vajhoej.dk> - 2011-11-25 21:22 -0500
            Re: Delete slot in the middle of an array and shift remaining Sven Fischer <svenfischer2@email.com> - 2011-11-26 13:48 +0100
              Re: Delete slot in the middle of an array and shift remaining Gene Wirchenko <genew@ocis.net> - 2011-11-26 21:25 -0800
        Re: Delete slot in the middle of an array and shift remaining Patricia Shanahan <pats@acm.org> - 2011-11-25 10:17 -0800
          Re: Delete slot in the middle of an array and shift remaining markspace <-@.> - 2011-11-25 10:24 -0800
          Re: Delete slot in the middle of an array and shift remaining Lew <lewbloch@gmail.com> - 2011-11-25 11:26 -0800
            Re: Delete slot in the middle of an array and shift remaining Arne Vajhøj <arne@vajhoej.dk> - 2011-11-25 21:17 -0500
          Re: Delete slot in the middle of an array and shift remaining Roedy Green <see_website@mindprod.com.invalid> - 2011-11-25 14:15 -0800
    Re: Delete slot in the middle of an array and shift remaining Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-25 08:08 -0500
    Re: Delete slot in the middle of an array and shift remaining markspace <-@.> - 2011-11-25 07:39 -0800
    Re: Delete slot in the middle of an array and shift remaining Roedy Green <see_website@mindprod.com.invalid> - 2011-11-25 13:35 -0800
    Re: Delete slot in the middle of an array and shift remaining Arne Vajhøj <arne@vajhoej.dk> - 2011-11-25 21:16 -0500

#10219 — Delete slot in the middle of an array and shift remaining

FromSvenfischer2@email.com (Sven Fischer)
Date2011-11-25 10:39 +0000
SubjectDelete slot in the middle of an array and shift remaining
Message-ID<4ecf7078$0$6582$9b4e6d93@newsspool3.arcor-online.net>
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

[toc] | [next] | [standalone]


#10220

FromLew <lewbloch@gmail.com>
Date2011-11-25 03:21 -0800
Message-ID<20684688.629.1322220072927.JavaMail.geo-discussion-forums@prnh1>
In reply to#10219
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

[toc] | [prev] | [next] | [standalone]


#10225

FromMagnus Warker <magnux@mailinator.com>
Date2011-11-25 18:44 +0100
Message-ID<jaok2t$vrs$1@news.m-online.net>
In reply to#10220
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

[toc] | [prev] | [next] | [standalone]


#10227

FromLew <lewbloch@gmail.com>
Date2011-11-25 09:55 -0800
Message-ID<25747787.150.1322243736771.JavaMail.geo-discussion-forums@prfi36>
In reply to#10225
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.

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.

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

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#10247

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-11-25 21:22 -0500
Message-ID<4ed04d71$0$289$14726298@news.sunsite.dk>
In reply to#10227
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

[toc] | [prev] | [next] | [standalone]


#10266

FromSven Fischer <svenfischer2@email.com>
Date2011-11-26 13:48 +0100
Message-ID<1pn1d7tkruj39ii166cuce2r4qli1oolnk@4ax.com>
In reply to#10247
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 <arne@vajhoej.dk>
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

[toc] | [prev] | [next] | [standalone]


#10276

FromGene Wirchenko <genew@ocis.net>
Date2011-11-26 21:25 -0800
Message-ID<sai3d7d1aekml8bkn2b0evd360n88t4ilj@4ax.com>
In reply to#10266
On Sat, 26 Nov 2011 13:48:17 +0100, Sven Fischer
<svenfischer2@email.com> 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

[toc] | [prev] | [next] | [standalone]


#10228

FromPatricia Shanahan <pats@acm.org>
Date2011-11-25 10:17 -0800
Message-ID<8-GdnXrqhJJORlLTnZ2dnUVZ_rqdnZ2d@earthlink.com>
In reply to#10225
On 11/25/2011 9:44 AM, Magnus Warker wrote:
> 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.

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:

public class ArrayDelete {

   public static void main(String[] args) {
     String[] slot = new String[5];

     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");
   }

}

[toc] | [prev] | [next] | [standalone]


#10229

Frommarkspace <-@.>
Date2011-11-25 10:24 -0800
Message-ID<jaomgl$utq$1@dont-email.me>
In reply to#10228
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");
> }
>
> }
>

[toc] | [prev] | [next] | [standalone]


#10231

FromLew <lewbloch@gmail.com>
Date2011-11-25 11:26 -0800
Message-ID<5805265.797.1322249183630.JavaMail.geo-discussion-forums@prnu24>
In reply to#10228
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

[toc] | [prev] | [next] | [standalone]


#10246

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-11-25 21:17 -0500
Message-ID<4ed04c28$0$289$14726298@news.sunsite.dk>
In reply to#10231
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

[toc] | [prev] | [next] | [standalone]


#10243

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-11-25 14:15 -0800
Message-ID<e830d79ogvtbjiskjguum7bfrjos9odnbf@4ax.com>
In reply to#10228
On Fri, 25 Nov 2011 10:17:50 -0800, Patricia Shanahan <pats@acm.org>
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. 

[toc] | [prev] | [next] | [standalone]


#10222

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-11-25 08:08 -0500
Message-ID<jao3vk$2qb$1@dont-email.me>
In reply to#10219
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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#10223

Frommarkspace <-@.>
Date2011-11-25 07:39 -0800
Message-ID<jaocr9$r91$1@dont-email.me>
In reply to#10219
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.

[toc] | [prev] | [next] | [standalone]


#10238

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-11-25 13:35 -0800
Message-ID<au10d7lg8vq323geunogvunmcgr9kb44p8@4ax.com>
In reply to#10219
On 25 Nov 2011 10:39:52 GMT, Svenfischer2@email.com (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. 

[toc] | [prev] | [next] | [standalone]


#10245

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-11-25 21:16 -0500
Message-ID<4ed04be5$0$289$14726298@news.sunsite.dk>
In reply to#10219
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web