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?

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

Looks good.

Simpler the better.

Thanks

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.

Edited by nick.crane: n/a

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

Edited by nick.crane: n/a