如何从C#中的数组列表中删除元素 [英] How to remove elements from array list in C#

查看:107
本文介绍了如何从C#中的数组列表中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从arraylist中删除元素后,它不会影响到arraylist。

在检索arraylist元素时,再次显示删除元素。

如何重新解决这个问题?



我的尝试:



After removing elements from arraylist,its not effecting to arraylist.
when retrieving the arraylist elements ,then again its showing the deleting elements.
How to reslove this problem?

What I have tried:

public class BaseEmployee
    {
        public int Id;
        public string Name;
    }

    public class Employee : BaseEmployee
    {
        public string Address;



        public Object Add()
        {

            Employee employeeDetail = new Employee();
            Console.Write("Employee Id:");
            employeeDetail.Id = int.Parse(Console.ReadLine());

            Console.Write("Employee Name:");
            employeeDetail.Name = Console.ReadLine();
            Console.Write("Employee Address:");
            employeeDetail.Address = Console.ReadLine();
            return employeeDetail;

        }

        static void Main(string[] args)
        {

            ArrayList arraylist = new ArrayList();
          


            Employee employeeDetails = new Employee();

            bool p = true;

            while (p)
            {

                Console.WriteLine("Employee Record Management System\n");
                Console.WriteLine("\n1)Add");
                Console.WriteLine("\n2)Edit");
                Console.WriteLine("\n3)Delete");
                Console.WriteLine("\n4)Dispaly");
                Console.WriteLine("\n5)Exit");
                Console.Write("\n Select opertaion:");
                int Option = int.Parse(Console.ReadLine());

                switch (Option)
                {
                    case 1:
                        arraylist.Add(employeeDetails.Add());

                        break;


                    case 4:
                        foreach (Object obj in arraylist)
                        {

                            Employee emp = obj as Employee;
                            Console.Write("{0}\t\t{1}\t\t{2}", emp.Id, emp.Name, emp.Address);
                            Console.Write("\n");
                            //Console.WriteLine(obj);
                        }


                        break;
            case 3:Console.Write("Enter  Employee Id:");
                        int EId = int.Parse(Console.ReadLine());
                        foreach(Object obj in arraylist)
                        {
                            Employee emp = obj as Employee;
                            if(EId==emp.Id)
                            {

                                arraylist.Remove(emp.Id);
                                arraylist.Remove(emp.Name);
                                arraylist.Remove(emp.Address);
                               

                            }
                        }

                          break;



                }

            }
        }

    }
}

推荐答案

你应该删除对象而不是对象的属性:

You should remove the object not the properties of the object :
...
          arraylist.Remove(emp);
...


我建​​议您使用List< Employee>而不是ArrayList。

你将避免使用Object(每次都使用它)并且你的代码将更容易阅读。

此外你将能够使用方法RemoveAll:



arraylist.RemoveAll(emp => emp.Id == EId);



我重写了你的代码片段,所以你可以评估我的建议:



I suggest you to use List<Employee> instead of ArrayList.
You'll avoid using Object (casting it every time) and your code will be easier to read.
Moreover you'll be able to use the method RemoveAll:

arraylist.RemoveAll(emp => emp.Id == EId);

I rewritten your code fragment, so you can evaluate my advice:

public class BaseEmployee
    {
        public int Id;
        public string Name;
    }
 
    public class Employee : BaseEmployee
    {
        public string Address;

        public static Employee Add()
        {
 
            var employeeDetail = new Employee();
            Console.Write("Employee Id:");
            employeeDetail.Id = int.Parse(Console.ReadLine());
 
            Console.Write("Employee Name:");
            employeeDetail.Name = Console.ReadLine();
            Console.Write("Employee Address:");
            employeeDetail.Address = Console.ReadLine();
            return employeeDetail;
 
        }
 
        static void Main(string[] args)
        {

            var arraylist = new List<Employee>();
          
            //Employee employeeDetails = new Employee();
 
            bool p = true;
 
            while (p)
            {
 
                Console.WriteLine("Employee Record Management System\n");
                Console.WriteLine("\n1)Add");
                Console.WriteLine("\n2)Edit");
                Console.WriteLine("\n3)Delete");
                Console.WriteLine("\n4)Dispaly");
                Console.WriteLine("\n5)Exit");
                Console.Write("\n Select operation:");
                int Option = int.Parse(Console.ReadLine());
 
                switch (Option)
                {
                    case 1:
                        arraylist.Add(Employee.Add());
 
                        break;
 

                    case 4:
                        foreach (Employee emp in arraylist)
                        {
 
                            //Employee emp = obj as Employee;
                            Console.Write("{0}\t\t{1}\t\t{2}", emp.Id, emp.Name, emp.Address);
                            Console.Write("\n");
                            //Console.WriteLine(obj);
                        }
 

                        break;
                    case 3:
                        Console.Write("Enter  Employee Id:");
                        int EId = int.Parse(Console.ReadLine());
                        
                        arraylist.RemoveAll(emp => emp.Id == EId);
                        
                        break;
                }
 
            }
        }
 
    }





再见



Bye


如果您要更改列表,则无法在 foreach 循环内执行此操作。 />


我的建议是使用linq找到它,如果找到则删除它。

If you're going to be changing the list, you can't do it inside a foreach loop.

My advice is to use linq to find it, and then remove it if found.
var found = arraylist.Where(x=>x.Id == EId).FirstOrDefault();
if (found != null)
{
    arraylist.Remove(found);
}


这篇关于如何从C#中的数组列表中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆