2013-03-26 74 views
-3

我无法理解ArrayList。我正在编写一个使用3类客户,视频和发票的程序。在下面的代码中,我创建了一个新的客户来添加设置ArrayList,但我觉得我正在试图把它看作是一个数组。我希望用户能够添加另一个客户对象并使用计数器“i”并遍历一系列问题以添加到该客户对象。我意识到这其中有些是相当混乱的。使用ArrayList在java中添加新对象?

import java.util.Scanner; 
import java.util.ArrayList; 

public class Prog4 { 


    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     ArrayList <Customer> customer = new ArrayList <Customer>(); 
     int i = 0; 
     char ans; 

     do 
     { 

      System.out.print("Customer name: "); 
      String name = in.next(); 
      customer.add(i,).setName(name); 

      System.out.print("Street Address: "); 
      String streetAddress = in.next(); 
      d1.setStreetAddress(streetAddress); 

      System.out.print("City: "); 
      String city = in.next(); 
      d1.setCity(city); 

      System.out.print("State: "); 
      String state = in.next(); 
      d1.setState(state); 

      System.out.print("Zipcode: "); 
      String zipcode = in.next(); 
      d1.setZipcode(zipcode); 

      System.out.print("Phone Number: "); 
      String phoneNumber = in.next(); 
      d1.setPhoneNumber(phoneNumber); 

      customer[i] = new Customer(name, streetAddress, city, state, zipcode, phoneNumber); 

      System.out.print("Would you like to enter in a new customer (y/n)? "); 
      String answer = in.next(); 
      ans = answer.charAt(0); 
     }while(ans == 'y'); 


    } 

} 
+1

什么是'customer.add(i,)'?这不能编译 – iberbeu 2013-03-26 23:05:34

回答

0

首先,我会建议设置值给客户对象,然后做客户。新增(Customer对象)

喜欢的东西:

import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 


public class Customer { 
    private String name; 
    private String streetAddress; 
    private String city; 
    private String state; 
    private String zipcode; 
    private String phoneNumber; 



    public String getName() { 
     return name; 
    } 



    public void setName(String name) { 
     this.name = name; 
    } 



    public String getStreetAddress() { 
     return streetAddress; 
    } 



    public void setStreetAddress(String streetAddress) { 
     this.streetAddress = streetAddress; 
    } 



    public String getCity() { 
     return city; 
    } 



    public void setCity(String city) { 
     this.city = city; 
    } 



    public String getState() { 
     return state; 
    } 



    public void setState(String state) { 
     this.state = state; 
    } 



    public String getZipcode() { 
     return zipcode; 
    } 



    public void setZipcode(String zipcode) { 
     this.zipcode = zipcode; 
    } 



    public String getPhoneNumber() { 
     return phoneNumber; 
    } 



    public void setPhoneNumber(String phoneNumber) { 
     this.phoneNumber = phoneNumber; 
    } 



    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     List <Customer> customerList = new ArrayList <Customer>(); 
     char ans; 

     do 
     { 
      Customer customer = new Customer(); 
      System.out.print("Customer name: "); 
      customer.setName(in.next()); 

      System.out.print("Street Address: "); 
      customer.setStreetAddress(in.next()); 

      System.out.print("City: "); 
      customer.setCity(in.next()); 

      System.out.print("State: "); 
      customer.setState(in.next()); 

      System.out.print("Zipcode: "); 
      customer.setZipcode(in.next()); 

      System.out.print("Phone Number: "); 
      customer.setPhoneNumber(in.next()); 

      customerList.add(customer); 

      System.out.print("Would you like to enter in a new customer (y/n)? "); 
      String answer = in.next(); 
      ans = answer.charAt(0); 
     }while(ans == 'y'); 

     for(Customer c: customerList){ 
      System.out.print(c.getName()); 

     } 

     for(int i=0; i<customerList.size(); i++){ 
      System.out.print(customerList.get(i).getName()); 

     } 


    } 

} 
+0

非常感谢你。这是一个很大的帮助。我在语法上遇到了麻烦。 – user2213630 2013-03-26 23:29:22

+0

满意为您服务。 – 2013-03-27 00:09:50

0

Java不支持运算符重载,Java标准库中的类只是普通的类。

这意味着使用[]唯一有意义的是数组(而不是ArrayList,它是Collections Framework中的类)。如果你需要知道ArrayList有什么方法可用,请参考:http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html(具体我想你对add()方法感兴趣)

0

首先,你应该重新命名

ArrayList <Customer> customer = new ArrayList <Customer>(); 

ArrayList <Customer> customers = new ArrayList <Customer>(); 

所以每个人(包括作者)都知道它是一些与客户的容器。

接着,这是一个语法错误:

customer.add(i,).setName(name); 

逗号用于分隔参数,如最大(2,4,5)。你正在添加一个数字,但这个容器只能有客户,而arraylist在添加时会自动增加索引,所以不用担心一些我。 所以,如果你想添加一个客户到ArrayList的客户,你首先需要创建一个。 一些可以工作...

public static void main(String[] args) 
{ 
    Scanner in = new Scanner(System.in); 
    ArrayList <Customer> customers = new ArrayList <Customer>(); 
    char ans; 

    do 
    { 

     System.out.print("Customer name: "); 
     String name = in.next(); 
     //customer.add(i,).setName(name); you dont need this 

     System.out.print("Street Address: "); 
     String streetAddress = in.next(); 
     //d1.setStreetAddress(streetAddress); 
     //these parameters are in constructor of class customer 

     ... 

     System.out.print("Phone Number: "); 
     String phoneNumber = in.next(); 
     //d1.setPhoneNumber(phoneNumber); 

     Customer customer = new Customer(name, streetAddress, city, state, zipcode, phoneNumber); 
     customers.add(customer); //first create, then add 

     System.out.print("Would you like to enter in a new customer (y/n)? "); 
     String answer = in.next(); 
     ans = answer.charAt(0); 
    }while(ans == 'y'); 


} 
0

而且上述的答案,请尝试以下操作:

import java.util.Scanner; 
import java.util.ArrayList; 

public class Prog4 { 

public static void main(String[] args) 
{ 
    Scanner in = new Scanner(System.in); 
    ArrayList <Customer> customers = new ArrayList <Customer>(); 
    // int i = 0; Why is this needed ? 
    char ans; 

    do 
    { 

     System.out.print("Customer name: "); 
     String name = in.next(); 

     System.out.print("Street Address: "); 
     String streetAddress = in.next(); 

     System.out.print("City: "); 
     String city = in.next(); 

     System.out.print("State: "); 
     String state = in.next(); 

     System.out.print("Zipcode: "); 
     String zipcode = in.next(); 

     System.out.print("Phone Number: "); 
     String phoneNumber = in.next(); 

     Customer tempCustomer = new Customer(name, streetAddress, city, state, zipcode, phoneNumber); 
     customers.add(tempCustomer); 

     System.out.print("Would you like to enter in a new customer (y/n)? "); 
     String answer = in.next(); 
     ans = answer.charAt(0); 
    }while(ans == 'y'); 

    in.close(); 
    } 

} 

注意,add()方法在ArrayList中添加一个元素到下一个索引,你不需要用方括号[]指定一个索引。

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html