Delete slot in the middle of an array and shift remaining

S

Sven Fischer

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
 
L

Lew

Sven said:
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.
 
E

Eric Sosman

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.
 
M

markspace

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.
 
L

Lew

Magnus said:
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?
 
M

markspace

It's a bit cleaner this way.


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

}
 
L

Lew

Patricia said:
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.
 
R

Roedy Green

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.
 
A

Arne Vajhøj

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 said:
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
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
S

Sven Fischer

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
 
G

Gene Wirchenko

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top