C#中使用ArrayList存储客户信息时重复调用Rent方法导致数据丢失的问题及数据展示需求
解决你的C#客户数据存储与展示问题
我仔细看了你的代码,发现几个关键问题导致数据无法保留,还需要补充展示功能,下面一步步给你梳理解决方案:
核心问题分析与修复
1. 数据无法保留的根源
你在Rent()方法内部创建ArrayList arr_cust,每次调用Rent都会新建一个空集合,之前的数据自然就丢失了。要解决这个,得把arr_cust提升为类的静态字段,让它在整个程序运行期间都存在,不会每次调用方法就被重新初始化。
2. 多余且错误的for循环
你的for循环for (int i = 0; i <= arr_cust.Count; i++)完全没必要,而且会导致同一个客户信息被重复添加两次——第一次添加后Count变成1,i=1还满足条件,又会加一次。直接去掉循环,单次调用arr_cust.Add()就行。
3. 递归返回主菜单的风险
用Main()递归回到主菜单会不断加深调用栈,次数多了容易触发栈溢出错误。更好的方式是把主菜单做成一个循环,让Rent()执行完后自动回到主菜单循环里。
新增展示所有客户数据的功能
添加一个DisplayCustomers()静态方法,遍历ArrayList里的所有客户信息,逐个格式化输出。
修改后的完整代码示例
using System; using System.Collections; namespace RentalEbikeSystem { class Program { // 把ArrayList声明为类级别的静态字段,全局保留数据 private static ArrayList arr_cust = new ArrayList(); static void Main(string[] args) { bool isRunning = true; while (isRunning) { Console.WriteLine("=== 电动自行车租赁系统 ==="); Console.WriteLine("1. 录入客户信息"); Console.WriteLine("2. 查看所有客户信息"); Console.WriteLine("0. 退出系统"); Console.Write("请选择操作:"); if (int.TryParse(Console.ReadLine(), out int choice)) { switch (choice) { case 1: Rent(); break; case 2: DisplayCustomers(); break; case 0: Console.WriteLine("感谢使用电动自行车租赁系统"); isRunning = false; break; default: Console.WriteLine("无效选项,请重新选择"); break; } } else { Console.WriteLine("输入无效,请输入数字"); } } } public static void Rent() { Console.WriteLine("Enter your first name"); string input_fname = Console.ReadLine(); Console.WriteLine("Enter your last name"); string input_lname = Console.ReadLine(); Console.WriteLine("Enter your contact number"); string input_phone = Console.ReadLine(); Console.WriteLine("Enter your payment method"); string input_payment = Console.ReadLine(); // 直接添加格式化后的客户信息,不需要循环 arr_cust.Add($"姓名:{input_fname} {input_lname},联系电话:{input_phone},付款方式:{input_payment}"); Console.WriteLine("客户信息已保存!"); Console.WriteLine("按任意键返回主菜单..."); Console.ReadKey(); } public static void DisplayCustomers() { Console.WriteLine("\n=== 所有客户信息 ==="); if (arr_cust.Count == 0) { Console.WriteLine("暂无客户数据"); } else { for (int i = 0; i < arr_cust.Count; i++) { Console.WriteLine($"{i+1}. {arr_cust[i]}"); } } Console.WriteLine("按任意键返回主菜单..."); Console.ReadKey(); } } }
额外优化建议
- 虽然
ArrayList可以用,但在C#中更推荐使用泛型集合List<string>(或者自定义Customer类,用List<Customer>),类型更安全,避免装箱拆箱的性能损耗。比如把private static ArrayList arr_cust = new ArrayList();改成private static List<string> arr_cust = new List<string>();,用法几乎一样,但更规范。 - 可以创建一个
Customer类来封装客户的各个字段,而不是把所有信息拼接成一个字符串,这样后续修改或查询单个字段会更方便:
public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string PhoneNumber { get; set; } public string PaymentMethod { get; set; } // 重写ToString方法,方便输出 public override string ToString() { return $"姓名:{FirstName} {LastName},联系电话:{PhoneNumber},付款方式:{PaymentMethod}"; } }
然后把集合改成private static List<Customer> customers = new List<Customer>();,添加的时候用:
customers.Add(new Customer { FirstName = input_fname, LastName = input_lname, PhoneNumber = input_phone, PaymentMethod = input_payment });
内容的提问来源于stack exchange,提问作者Taewan Vitooreangrong
相关产品推荐
相关产品推荐

