1

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

1
  • Actually, you could do something along the lines of data = (new List<T>(data)).RemoveValue(value).ToArray(), but that would be very(!) inefficient, so better use a List<T> instead as shown in the answers. Commented Feb 28, 2011 at 12:02

3 Answers 3

12

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());
Sign up to request clarification or add additional context in comments.

2 Comments

... or LinkedList<T> or Hashset<T>. You should see what you need from this private data, and choose the correct structure for the task.
I don't think that this was the point of the exercise...
1

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();
}

Comments

1

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;
}

1 Comment

Helpful. Besides, this indeed answers the original question

Your Answer

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