Delete element of a class arrangement C # (. net)

0

I was searching the internet to see if I could find anything but I could not find anything, and unfortunately my knowledge is still low, which is why I came here again.

I tell you, I have a class library with a class called "professor" with its respective variables, and a class arrangement called "Duos" where I have a teacher-type array with their respective methods. I have a method to add " teacher "to the arrangement and also look for a" teacher "by the attribute rut.

method add:

public string AgregarProfesor(Profesor p)
{
     if (buscar(p.Rut) == -1)
     {
          Array.Resize(ref _profesor, Profesor.Length + 1);
          _profesor[Profesor.Length - 1] = p;
          return "\n\nSe agregó nuevo profesor.";
     }
     return "\nEl Profesor ya existe, No se agregó.";
}

method search:

public int buscar(string rut)
{
     for (int i = 0; i < _profesor.Length; i++)
     {
           if (Profesor[i].Rut == rut)
               return i;
            }
            return -1;
      }
}

I need to create a method which allows me to use the search method to find a teacher and remove it from the fix, but I have tried several things and searched the internet and the only thing I find is a list of fixes, all help is Welcome, thank you very much.

    
asked by Jordan Blake Told 07.05.2017 в 17:15
source

3 answers

1

As you have been told, the Array is not the most appropriate object to maintain a collection of elements in which you want to add or remove elements in any position.

The cost of resizing the Array and rearranging its elements is very large and you have other types that are more appropriate to perform this type of operation.

If, as you explain, for academic reasons it must be an Array I think that the best option to eliminate an element, for cost and clarity of code, is to generate a new Array with the remaining elements.

Look at this example:

var arrNumbers = new int[]{ 1, 2, 3, 4, 5, 6};
// Eliminar el 4:
arrNumbers = arrNumbers.Where(i => i != 4).ToArray();
// arrNumbers ->  int[5] { 1, 2, 3, 5, 6 }

To eliminate the number 4 from the arrNumbers Array what I'm doing is create a new Array with all the elements that are not number 4 and assign it to the variable arrNumbers.

    
answered by 08.05.2017 в 09:53
0

I think it would be something like

public void EliminarProfesor(Profesor p) { 
    //si es diferente de -1 es por que si existe entonces si podes eliminar 
    if(buscar(p.Rut)!=-1) { 
        int index = buscar(p.Rut); 
        //guardo el index en el que se encuentra el profe 
        _profesor.removeAt(index); //elimino el profesor en esa posicion
    }
}
    
answered by 07.05.2017 в 20:28
0

There is no method to remove elements from an Array in C # or .Net, not unless you have found it. In general, an Array is used for a certain amount of elements, which in any case can be enlarged, but not made smaller. For more information on fixes you can see this tutorial Tutorial Arrangements on MSDN .

If you want to delete elements you should use ArrayList or List. The ArrayList and List have the RemoveAt () methods that remove the element in the indicated position or Remove () that removes the first element that matches the last string.

By methods to delete in ArrayList you can see this link ArrayList Remove Methods .

    
answered by 07.05.2017 в 21:26