Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Delete slot in the middle of an array and shift remaining

Reply
Thread Tools

Delete slot in the middle of an array and shift remaining

 
 
Sven Fischer
Guest
Posts: n/a
 
      11-25-2011
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

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      11-25-2011
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-25-2011
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-25-2011
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.
 
Reply With Quote
 
Magnus Warker
Guest
Posts: n/a
 
      11-25-2011
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-25-2011
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

 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-25-2011
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");
> }
>
> }
>


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-25-2011
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-25-2011
On 25 Nov 2011 10:39:52 GMT, (E-Mail Removed) (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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-25-2011
On Fri, 25 Nov 2011 10:17:50 -0800, Patricia Shanahan <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java left shift and right shift operators. Sanny Java 38 04-29-2011 10:02 PM
What happens when I delete with Shift+Delete Sridhar Panatula Windows 64bit 6 10-06-2008 09:29 AM
Shift./Enter in address slot Perk Firefox 2 02-09-2005 12:46 AM
Shift - byte[] buf shift Roberto Gallo Java 3 01-27-2004 04:26 PM
left shift then right shift an unsigned int Wenjie C++ 3 07-11-2003 08:22 PM



Advertisments