1

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?

Votes + Comments
Good :)
4
Contributors
10
Replies
12
Views
7 Years
Discussion Span
Last Post by nick.crane
Featured Replies
  • 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 Read More

0

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

0

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.

Edited by nick.crane: n/a

0

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.

0

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];
            }
0

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 .

0

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

2

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

Edited by nick.crane: n/a

Votes + Comments
Thank you :)
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.