简体   繁体   中英

Remove an item from generic Array C#

I have a generic class defined like this

class MyGenericClass<T> where T : ICompareable
{
  T[] data;

  public AddData(T[] values)
  {
     data = values;
  }
}

In mainForm, I create 3 random numbers, and add them as values, lets say 1, 2 and 3. So my T[] data; will look like this: [0]1 [1]2 [2]3

What I want to do is to remove 1 of these values from the array, how do I do that when I'm using generics. Lets say I want to remove 3 from the array so it would look like this[0]1 [1]2

Why don't you use a generic List ( List<T> ) instead of the array as a private member of your class to hold the data ?

As it is a private member, the 'outside world' cannot access the list, and you will have a much easier life since a List allows you to Add and Remove items easily.

class MyGenericClass<T> where T : ICompareable
{
  private List<T> data = new List<T>();

  public AddData(params T[] values)
  {
      data.AddRange (values);
  }

  public RemoveData( T value )
  {
     data.Remove (value);
  }

  public RemoveData( params T[] values )
  {
      for( int i = 0; i < values.Length; i++ ) 
      {
          data.Remove (values[i]);
      }
  }
}

Once you've done this, you can use the Add member-method of the List to add items, and the Remove member method to remove items. Simple as that.

I've used the params keyword in the AddData method so that you can do this:

var x = new MyGenericClass<int>();

x.AddData(1);
x.AddData(2, 3, 4);
x.AddData(somIntegerList.ToArray());

I kind of had the same question, because I was writing a dynamically sized array to practice creating generic classes.

I found that you can either: move all of the elements down and then set the last element equal to default(T) , or create a new array of size-1 to be filled with the remaining elements.

Ex:

public class Array<T>
{
    private T[] _array { get; set; }
    private int _max { get; set; }
    private int _size { get; set; }

    public Array()
    {
        _max = 10;
        _array = new T[_max];
        _size = 0;
    } 

    public T Remove(int i)
    {
        if (i >= _size || i < 0) return default(T);

        var tmp = _array[i];

        for (var j = i; j < _size-1; ++j)
        {
            _array[j] = _array[j + 1];
        }
        _array[_size - 1] = default(T);   
        _size--;
        return tmp;
    }
}

Or...
public T Remove(int i) {
  var tmp = new T[_size-1];

  for(var j=0; j < i; ++j) 
  {
     tmp[j] = _array[j];
  }

  var result = _array[i];

  for(var j=i+1; j < _size-1; ++j) 
  {
     tmp[j] = _array[j];
  }
  _array = null;
  _array = tmp;
  return result;
}

Change your class to look like this (I also implemented Frederik's suggestion of using a List instead of a array.

class MyGenericClass<T> where T : ICompareable
{
  List<T> data;

  public AddData(T value)
  {
     data.Add(value);
  }
  public RemoveData(T value)
  {
     data.Remove(value);
  }
}

If for some reasaon, you insist on using an array, the remove method may look something like this

public RemoveData(T value)
{
  data = data.Where( e => e.CompareTo(value) != 0).ToArray();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM