Problem
Let’s pretend I have this collection of items.
int[] numbers = {1, 3, 4, 9, 2};
What is the best way to delete an element based on its “name”? Let’s say you’re at number four.
Isn’t it true that even ArrayList couldn’t assist you delete?
string strNumbers = " 1, 3, 4, 9, 2";
ArrayList numbers = new ArrayList(strNumbers.Split(new char[] { ',' }));
numbers.RemoveAt(numbers.IndexOf(4));
foreach (var n in numbers)
{
Response.Write(n);
}
Asked by ahmed
Solution #1
If you don’t know the index, you can eliminate all instances of 4 by typing:
LINQ is a query language that allows you to express your thoughts (.NET Framework 3.5)
int[] numbers = { 1, 3, 4, 9, 2 };
int numToRemove = 4;
numbers = numbers.Where(val => val != numToRemove).ToArray();
LINQ is not supported (.NET Framework 2.0)
static bool isNotFour(int n)
{
return n != 4;
}
int[] numbers = { 1, 3, 4, 9, 2 };
numbers = Array.FindAll(numbers, isNotFour).ToArray();
If you only want to get rid of the first incidence, follow these steps:
LINQ: LINQ is a query language (.NET Framework 3.5)
int[] numbers = { 1, 3, 4, 9, 2, 4 };
int numToRemove = 4;
int numIndex = Array.IndexOf(numbers, numToRemove);
numbers = numbers.Where((val, idx) => idx != numIndex).ToArray();
LINQ is not supported (.NET Framework 2.0)
int[] numbers = { 1, 3, 4, 9, 2, 4 };
int numToRemove = 4;
int numIdx = Array.IndexOf(numbers, numToRemove);
List<int> tmp = new List<int>(numbers);
tmp.RemoveAt(numIdx);
numbers = tmp.ToArray();
Edit: In case you hadn’t worked it out yet, the LINQ code samples require the.NET Framework 3.5, as pointed out by Malfist. If you’re aiming for 2.0, you’ll need to look at the Non-LINQ examples.
Answered by BenAlabaster
Solution #2
int[] numbers = { 1, 3, 4, 9, 2 };
numbers = numbers.Except(new int[]{4}).ToArray();
Answered by meetjaydeep
Solution #3
You can alternatively change your array to a list and remove the items from it. After that, you can return to your array.
int[] numbers = {1, 3, 4, 9, 2};
var numbersList = numbers.ToList();
numbersList.Remove(4);
Answered by Dave DP
Solution #4
There is an error in the code that is written in the question.
Strings of ” 1″ ” 3″ ” 4″ ” 9″ and ” 2″ appear in your arraylist (note the spaces)
Because 4 is an int, IndexOf(4) will return null, and even “tostring” will convert it to “4” rather than “4”, thus nothing will be eliminated.
To achieve your goal, you should use an arraylist.
Answered by DevinB
Solution #5
Here is where I put my answer.
This is a method of deleting an array element without copying it to another array – only within the same array instance:
public static void RemoveAt<T>(ref T[] arr, int index)
{
for (int a = index; a < arr.Length - 1; a++)
{
// moving elements downwards, to fill the gap at [index]
arr[a] = arr[a + 1];
}
// finally, let's decrement Array's size by one
Array.Resize(ref arr, arr.Length - 1);
}
Answered by infografnet
Post is based on https://stackoverflow.com/questions/496896/how-to-delete-an-element-from-an-array-in-c-sharp