Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello there, i have an array that containe those data
C#
int [] marks = new int[5]  { 1, 2, 3, 4, 5};

i want to destroy the seconde ellement the "2"

and i want to get my array like thise
marks =  { 1,  3, 4, 5, 0};

or marks ={1,3,4,5}


please help

What I have tried:

Hello there, i have an array that containe those data
C#
int [] marks = new int[5]  { 1, 2, 3, 4, 5};

i want to destroy the seconde ellement the "2"

and i want to get my array like thise
marks =  { 1,  3, 4, 5, 0};

or marks ={1,3,4,5}


please help
Posted
Updated 31-May-16 8:22am
Comments
Afzaal Ahmad Zeeshan 31-May-16 12:56pm    
Just use a generic list of integer type and remove the element that you don't want. Very less people use static arrays now a days.
Philippe Mori 31-May-16 20:52pm    
You should not use an array if you want to modify it. Use a list instead. By the way, in .NET a list is essentially a resizable array.

If you need an array later in code, then convert back to an array once all modifications are done. If it is your own code, it would be preferable to refactor it to use list everywhere you need to modify the number of items after creation.

Essentially, you should always try to select the container that is the most appropriate for the problem.

If you must modify an array in place then when you delete an element, you need to copy all of the following elements down one position, and put the zero at the end position:
C#
int [] marks = new int[5]  { 1, 2, 3, 4, 5};
int deleteAtIndex = 1;
for (int i = deleteAtIndex; i < marks.Length - 1; ++i)
{
  marks[i] = marks[i + 1];
}
marks[marks.Length - 1] = 0;

There is no way to resize an array in place.

If you can just create a new array with the element removed then there are several ways to do this:
(Starting with the same marks array and deleteAtIndex as above.)
C#
int [] marksNew = new int[marks.Length-1];
for (int i = 0, j = 0; i < marks.Length; ++i, ++j)
{
  if (i != deleteAtIndex)
    marksNew[j] = marks[i];
  else
    ++i;  
}
marks = marksNew; 

or (using Linq):
C#
marks = marks.Where((_, ix) => ix != deleteAtIndex).ToArray();

or using generic List:
C#
List<int> temp = new List<int>(marks);
temp.RemoveAt(deleteAtIndex);
marks = temp.ToArray();


Edit:
It wasn't clear if you want to remove the element based on knowing the position in the array or the value of the element. The above assume that you know the position (index) of the element to be removed. If you want to remove an element based on its value, then (for the in place removal):
C#
int deleteValue = 2;
int deletedCount = 0;
for (int i = 0, j = 0; i < marks.Length; ++i, ++j)
{
  int mi = marks[i];
  if (mi != deleteValue)
  {
    marks[j] = mi;
  }
  else
  {
    ++i;
    ++deletedCount;
  }
}
for (int i = 0, j = marks.Length - 1; i < deletedCount; ++i, --j)
  marks[j] = 0;

for the replacement array case:
C#
marks = marks.Where(v => v != deleteValue).ToArray();

or using generic List:
C#
List<int> temp = new List<int>(marks);
temp.Remove(deleteValue);
marks = temp.ToArray();

As Afzaal Ahmad Zeeshan said in the initial comments, using a generic List instead of an array (if you can) makes this much easier.
 
Share this answer
 
v5
Comments
Jammes_Ca 31-May-16 14:20pm    
hi thanks for the answer, i want to remove the element based on knowing the position in the array ?? ho wdo i do that please
Matt T Heffron 31-May-16 14:41pm    
OK, then the code in the first half of my solution are ways to do that covering both modifying the array in place, and making a new array with the element removed.
The only reason you should need to modify an array in place would be if there is some other part of the code that already has a reference to that array and there's no way to have that reference updated.
If this is the only reference to the array and making a replacement array is OK then the second, third and fourth code blocks above are your options.
If you can switch to declaring marks as a List<int> instead of a int[] then just using marks.RemoveAt(...) is the ultimately simple solution!!
Jammes_Ca 31-May-16 16:36pm    
hi thanks but i want to work with array not list
Matt T Heffron 31-May-16 17:22pm    
OK, then for just an array, use the second block of code above.
(The block starting with int [] marksNew .)
That will create a new array which is a copy with the specified element omitted.
At the end of that block, the marks is replaced with marksNew.
As I noted, to do it in place, you cannot change the size of the array and must fill the tail of the array with some value to indicate invalid. Or you must keep a usable length value that you reference everywhere you use marks so you know how much of it is valid.
Philippe Mori 31-May-16 20:46pm    
You should really uses list which in C# behave almost as arrays except that items can be added or removed after creation.

If you need to use array elsewhere in code, you probably want to use une a temporary list and convert the list to an array at the end when all modifications are done.
You cannot "destroy an element of an array". First, an array is something immutable in .NET, if you want to modify its size you will have to create a new one and copy the data. Moreover, your array contains only int values, which are value types, and there is no concept of destroying a value type.

Your best option is to use Linq extensions for that:
C#
int [] marks = new int[5]  { 1, 2, 3, 4, 5};
return marks.Where(v => v != 2).ToArray(); // You get {1,3,4,5}
 
Share this answer
 
try this
C#
int removeItem = 2;
int[] marks = new int[5] { 1, 2, 3, 4, 5 };
marks = marks.Where(k => k != removeItem).ToArray();


Note : add the reference
C#
using System.Linq;



Remove based on position, take care of validation.
C#
int position = 2;
int[] marks = new int[5] { 1, 2, 3, 4, 5 };
var temp = marks.ToList();
temp.RemoveAt(position-1);
marks = temp.ToArray();
 
Share this answer
 
v2
any reason you dont want use ArayList instead Array ?.. below code converts Array to ArrayList and converts back Array finally destroys Arraylist Obj ..

if you use array List ..all need
C#
al.RemoveAt(1);


for ur requirement

C#
int[] marks = new int[5] { 1, 2, 3, 4, 5 };

      ArrayList al= new ArrayList();

      al.AddRange(marks);
      al.RemoveAt(1);

      marks = (int[])al.ToArray(typeof(int));

      al.Clear();
      al = null;
 
Share this answer
 
Comments
Matt T Heffron 31-May-16 13:51pm    
ArrayList is almost absolete as it isn't type-safe.
Generic List<T> is a much better choice.
(ArrayList is really only a better choice if the contents are mixed types.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900