简体   繁体   English

从通用数组C#中删除一项

[英]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; 在mainForm中,我创建3个随机数,并将它们添加为值,例如1、2和3。 will look like this: [0]1 [1]2 [2]3 看起来像这样:[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 可以说我想从数组中删除3,所以它看起来像这样[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 ? 为什么不使用通用列表( List<T> )而不是数组作为类的私有成员来保存数据?

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. 完成此操作后,可以使用ListAdd成员方法添加项目,并使用Remove成员方法删除项目。 Simple as that. 就那么简单。

I've used the params keyword in the AddData method so that you can do this: 我在AddData方法中使用了params关键字,因此您可以执行以下操作:

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. 我发现您可以执行以下操作:向下移动所有元素,然后将最后一个元素设置为default(T) ,或创建一个大小为1的新数组,以填充其余元素。

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. 更改您的类,使其看起来像这样(我还实现了Frederik的使用List而不是数组的建议。

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 如果对于某些原因,您坚持使用数组,则remove方法可能看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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