gpt4 book ai didi

java - 数组列表java程序

转载 作者:行者123 更新时间:2023-12-01 22:17:39 26 4
gpt4 key购买 nike

帮助使用数组列表编写程序,该数组列表存储名称、地址、电话号码、日期和时间(每个客户)的值,稍后我需要检索特定信息,例如指定日期的所有客户姓名。如有任何帮助,我们将不胜感激。

代码:

public class Details {
public static void main(String args[]) throws IOException {
InputStreamReader rdr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(rdr);
String s;
s = br.readLine();
System.out.println("PLEASE ENTER CLIENT NAME");
String name = br.readLine();
System.out.println("PLEASE ENTER CLIENT ADDRESS");
String add = br.readLine();
System.out.println("PLEASE ENTER CLIENT CONTACT PHONE NUMBER");
String pnum = br.readLine();
List list = new ArrayList();
list.add("name");
list.add("add");
list.add("pnum");
list.add("food");
}
}

最佳答案

像这样创建一个类Customer -

public class Customer{
private String name;
private String address;
private String phoneNumber;
private Date date;

public Customer(name, address, phoneNumber, date){

this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.date = date;
}

//getters and setters method

}

之后,您创建一个像这样的 Customer ArrayList -

List<Customer> `customerList` = new ArrayList<Customer>();  

现在创建一个 Customer 的对象/实例,如下所示 -

Customer aCustomer = new Customer("ranjit", "someAddress", "023-859 74", new Date() );

然后将 Customer 对象/实例 aCustomer 添加到 CustomerArrayList - customerList像这样:

customerList.add(aCustomer);

通过给定的方式,您可以更轻松地处理客户。现在您有一个包含所有客户属性(姓名、地址、电话号码等)的实体。因此,您不需要将所有属性/属性存储在单独的 ArrayList

关于java - 数组列表java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30679665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com