I have an array of strings.

string[] array = new string[] { "0", "1", "2", "3", "4", "5", "6" };

I would like to remove a string from the array above using a variable removeIndex, let's say it is equal to 3.

int removeIndex = 3;

I now have to rearrange my array so that all the strings are moved back by one index.

int end = array.Length - 1;

            for (int i = removeIndex; i < end; ++i)
            {
                array[i] = array[i + 1];
            }

The last index is now null.

array[end] = null;

Is this the best approach to moving all objects back one space in an array? What would you change if not?

finito commented: Good :) +2

Recommended Answers

All 10 Replies

Looks good.

Simpler the better.

commented: Thanks +1

OK, let's step it up one notch.

Imagine I now have the same array but accessed using 2D array methodology.

string[] array = new string[] { "0", "1", "2", "3", "4", "5" };

The x dimension is 2 and the y dimension is 3 so the array can be seen as

// "0" "1"
            // "2" "3"
            // "4" "5"

            int xDim = 2;
            int yDim = 3;

If I wish to access the array I can use the following to convert the 2 dimensions to one dimension

index = yIndex * xDim + xIndex

How can I remove the same index (3) and change the loop to only iterate through the same column and move the indices in that dimension back one place?

I.e.

// "0" "1"
// "2" "3"
// "4" "5"

to

// "0" "1"
// "2" "5"
// "4" null

I'd let the compiler manage the indexing for you by declaring the array as 2D.

string[,] array = new string[,] { { "0", "1" }, { "2", "3" }, { "4", "5" } };

Then your code will be very similar to the 1D version, just add the extra index for the column.

If you plan to add and remove elements i would suggest looking at generic lists. You can create a List<string> then add and remove elements. It resizes dynamically and automatically moves the elements down when one is removed.

I'd let the compiler manage the indexing for you by declaring the array as 2D.

Can't do that as XML serializing doesn't handle 2D arrays.

Can I not use something like this?

for (int i = removeIndex; i < end; i += xDim)
            {
                array[i] = array[i + xDim];
            }

Yes, that will also work.
You are better having i scopped outside the for loop so you can use its last value to set the null.
Also you may need to adjust end by - XDim instead of just - 1 .

I see. The following

string[] array = new string[] { "0", "1", "2", "3", "4", "5" };

            // 0 1
            // 2 3
            // 4 5

            int xDim = 2;
            int yDim = 3;

            // y * xDim + x

            int i;         
            int end = array.Length - xDim;
            int removeIndex = 3;

            for (i = removeIndex; i < end; i += xDim)
            {
                array[i] = array[i + xDim];
            }

            array[i] = null;

gives me "0", "1", "2", "5", "4".

I've done something wrong...

No, that looks right.

// "0" "1"
// "2" "3"
// "4" "5"
// i.e. "0", "1", "2", "3", "4", "5"

to

// "0" "1"
// "2" "5"
// "4" null
// i.e. "0", "1", "2", "5", "4", null

commented: Thank you :) +1

Oh yeah. My brain failed. Thank you.

Actually, when I tried it, I also thought it had gone wrong at first.:$
Our brains must expect number sequences to be in order!;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.