0

I am using a regular array which has 5 elements I want to remove the element but I don't want to use any collection such as arrayList or List

My code is

for (int c = 0; c < Alpha.Length; c++)
{
    if (Alpha[c].Power< 0)
    {
      //  Alpha.s = null;
    }
}
3
  • 6
    Why you don't want to use List<T> instead? Commented Sep 9, 2013 at 12:16
  • 1
    You can't remove element from regular array. All you can - assign some value to element, or create new array and copy all other items there. That's why collections are handy Commented Sep 9, 2013 at 12:17
  • You want to remove an item and sort the others, so the array dont get null values on is middle? Commented Sep 9, 2013 at 12:41

3 Answers 3

10

From MSDN

The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance.

(Emphasis mine).

Arrays, by design, have a fixed size in C#, so a new array should be allocated:

Alpha = Alpha.Where(a => a.Power >= 0).ToArray();

Without Linq:

TheType[] alpha2 = new TheType[Alpha.Length];
int j=0;
for (int i=0; i < Alpha.Length; i++)
    if(Alpha[i].Power >= 0)
    {
        alphaTmp[j++] = Alpha[i];
    }
Array.Resize(ref alphaTmp, j);
Alpha = alphaTmp;
Sign up to request clarification or add additional context in comments.

5 Comments

It will create new array with proper elements. But because you can't change array size without recreating it I think it's really a good way to go here (but using List<T> would be better anyway).
There is no use of collections here. There is only the use of linq.
@user2740970 Not LINQ either? You certainly have strict (and strange) requirements.
thats the problem no linq and collection otherwise i would have used List<>
What version of .Net are you targeting?
6

You can't "remove" an element from an array in terms of any operation which changes the length of the array: once an array has been created, its length is fixed. Options:

  • Change the element value to null, or some other appropriate value
  • Shift all the other elements towards the head and set the last element value to null etc
  • Create a new array with a smaller length, potentially assigning it back to the variable/property which originally referred to the larger array

It's unclear why you're rejecting the normal collections though. You should think about this decision very carefully - using the built-in collection types is almost always a better solution.

Comments

1

You can use LINQ:

Alpha = Alpha.Where(ob => bo.Power >= 0).ToArray();

Remember - it will create a new array.

BTW. Why you don't want to use List<T>?

1 Comment

i dont want to use collection and linq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.