-1

我在使用删除功能时遇到问题。除了删除功能,我一切正常。

任何帮助,将不胜感激。我试图改变它一点,但它仍然没有删除。我不知道我的删除功能有什么问题。

public class Person
{
    private string name;
    private string phoneNumber;

    public string Name
    {
        set { name = value; }
        get { return name; }
    }
    public string PhoneNumber
    {
        set { phoneNumber = value; }
        get { return phoneNumber; }
    }
    public void PrintInfo()
    {
        Console.WriteLine();
        Console.WriteLine(" Name: {0}", name);
        Console.WriteLine(" Phone Number: {0}", phoneNumber);
        Console.WriteLine();
    }
    public void SaveASCII(ref StreamWriter output)
    {

        output.WriteLine(name);
        output.WriteLine(phoneNumber);

    }
    public void LoadASCII(ref StreamReader input)
    {

        name = input.ReadLine();
        phoneNumber = input.ReadLine();

    }

}


public class membershipList
{

    public Person[] ML = null;

    public void AddMember(Person p)
    {
        if (ML == null)
        {
            ML = new Person[1];
            ML[0] = p;
        }
        else
        {
            Person[] temp = ML;
            ML = new Person[temp.Length + 1];
            for (int i = 0; i < temp.Length; ++i)
            {
                ML[i] = new Person();
                ML[i] = temp[i];
            }
            ML[temp.Length] = new Person();
            ML[temp.Length] = p;
            temp = null;

        }
    }
    public void DeleteMember(string p)
    {

        if (ML != null)
        {
            foreach (Person pers in ML)
            {
                if (pers.Name.ToLower().CompareTo(p.ToLower()) == 0)
                {
                  pers.Remove();
                  break;

                }
            }
        }

        else Console.WriteLine("Then list is empty.");
    }


                //   {
                //      int memberIndex = Array.FindIndex(ML, p => p.Name == name);
                //      if (memberIndex == -1)
                //    {
                //        Console.WriteLine(name + " had not been added before.");
                //      return;
                //   }
               // else
               // {
                //    List<Person> tmp = new List<Person>(ML);
               //     tmp.RemoveAt(memberIndex);
               //     ML = tmp.ToArray();
               // }
       // }
  //  }


    public void PrintAll()
    {
        if (ML != null)
            foreach (Person pers in ML)
                pers.PrintInfo();
        else Console.WriteLine("Then list is empty");
    }
    public void Search(string p)
    {
        if (ML != null)
        {
            foreach (Person pers in ML)
            {
                if (pers.Name.ToLower().CompareTo(p.ToLower()) == 0)
                {
                    Console.WriteLine("1 Record Found:");
                    pers.PrintInfo();
                    break;
                }
            }
        }

        else Console.WriteLine("Then list is empty.");
    }

    public void ReadASCIIFile()
    {
        StreamReader input = new StreamReader("memberlist.dat"); ;

        try
        {

            int num = Convert.ToInt32(input.ReadLine());
            ML = new Person[num];

            for (int i = 0; i < num; ++i)
            {
                ML[i] = new Person();
                ML[i].LoadASCII(ref input);

            }
            input.Close();
        }
        catch (FormatException e)
        {
            Console.WriteLine(e.Message);
            input.Close();
        }


    }

    public void SaveASCIIFile()
    {

        StreamWriter output = new StreamWriter("memberlist.dat");

        output.WriteLine(ML.Length);
        foreach (Person pers in ML)
        {
            pers.SaveASCII(ref output);

        }
        output.Close();


    }
}


class Program
{
    static void Main(string[] args)
    {

        membershipList ML = new membershipList();
        ML.ReadASCIIFile();

        string option;
        do
        {

            // Console.Clear();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("MemberShip List MENU");
            Console.WriteLine();
            Console.WriteLine("     a. Add");
            Console.WriteLine("     b. Seach");
            Console.WriteLine("     c. Delete");
            Console.WriteLine("     d. Print All");
            Console.WriteLine("     e. Exit");
            Console.WriteLine();
            Console.Write("option: ");
            option = Console.ReadLine().ToLower();

            switch (option)
            {
                case "a":
                    Person np = new Person();
                    Console.Write("Enter Name: ");
                    np.Name = Console.ReadLine();
                    Console.Write("Enter PhoneNumber: ");
                    np.PhoneNumber = Console.ReadLine();
                    ML.AddMember(np);
                    break;
                case "b":
                    Console.Write("Enter Name: ");
                    string name = Console.ReadLine();
                    ML.Search(name);
                    break;
                case "c":
                    Console.Write("Enter Name to be Deleted:");
                    string pers = Console.ReadLine();
                    ML.DeleteMember(pers);
                    break;
                case "d":
                    ML.PrintAll();

                    break;
                case "e":
                    ML.SaveASCIIFile();
                    Console.WriteLine("BYE...... ");
                    break;

                default:
                    Console.WriteLine("Invalid Option");
                    break;
            }


        } while (option.ToLower() != "d");
    }
}
4

3 回答 3

0

我看到两件事:

  1. 如果要从列表中添加和删除项目,请使用 aList<Person>而不是数组。就目前而言,您需要将数组复制到临时列表,删除项目,然后复制回数组。
  2. 然后,您的Delete功能可以是:

    if (ML != null)
    {
        Person pers = ML.FirstOrDefault(x => x.Name.ToLower() == p.ToLower());
        if(pers != null)
        {
              ML.Remove(pers);
              break;
        }
    }
    
于 2013-09-01T03:34:20.627 回答
0

你的问题是这样的:

foreach (Person pers in ML)
{
    if (pers.Name.ToLower().CompareTo(p.ToLower()) == 0)
    {
        pers.Remove();
        break;
    }
}

您正在从正在迭代的集合中删除,这会抛出循环的迭代器。

要么向后循环遍历集合,因此删除不会抛出循环的迭代器。

或者

创建第二个列表,其中包含您要保留的值。因此,本质上,您会做相反的事情;而不是删除您将不会添加到保留列表中。

于 2013-09-01T03:37:18.437 回答
0

您正在遍历一个数组,然后对该人调用 remove 。关于 for each 循环的一件事是它使用了在 for each 期间无法修改的迭代器。但即使你可以,它也不是那么简单,因为你不能简单地从数组中删除。您可以从通用 List 或 ListArray 中执行此操作,但不能从数组中执行此操作。要遵循您已经遵循的相同模式(假设您需要使用数组并调整它们的大小),您需要创建一个临时数组并将 ML 数组中的所有元素复制到新数组中减去已删除的成员。

检查我的语法,因为我没有测试这段代码,但你应该明白了。

public void DeleteMember(string p)
    {

        if (ML != null)
        {
            Person[] tempList = new Person[ML.Length - 1];
            bool deletedAPerson = false;
            int i = 0;
            foreach (Person pers in ML)
            {

                if (pers.Name.ToLower().CompareTo(p.ToLower()) == 0)
                {
                   deletedAPerson = true;
                   continue; //skip over this person and do not copy over to the new Array
                }

                if(i < pers.Length) //if no person was found, prevent index out of bounds exception
                    tempList[i++] = pers;
            }
            if(deletedAPerson)
               ML = tempList;
        }

        else Console.WriteLine("Then list is empty.");
    }
于 2013-09-01T04:33:55.700 回答