Arraylist java程序

时间:2015-06-06 06:21:39

标签: java oop arraylist

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

代码:

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");
    }
}

1 个答案:

答案 0 :(得分:3)

像这样创建一个类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添加到ArrayList的{​​{1}} - Customer,如下所示:

customerList

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