-3

我无法理解 ArrayList。我正在使用 3 个类客户、视频和发票编写程序。在下面的代码中,我正在创建一个新客户以添加到设置 ArrayList,但我觉得我正在尝试将它视为一个数组。我希望用户能够添加另一个客户对象并使用计数器“i”并运行一系列问题以添加到该客户对象。我意识到其中一些是一团糟。

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');


    }

}
4

4 回答 4

0

首先,你应该重命名

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

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

所以每个人(包括作者)都知道这是一个装有客户的容器。

接下来,这是一个语法错误:

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

逗号用于分隔参数,例如 max(2, 4, 5)。您正在添加一个数字,但是这个容器里面只能有客户,而且 arraylist 在添加时会自动增加索引,所以不要担心一些 i。因此,如果您想将客户添加到 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');


}
于 2013-03-26T23:19:48.497 回答
0

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

这意味着使用 [] 唯一有意义的是数组(而不是 ArrayList,它是 Collections Framework 中的一个类)。如果您需要知道 ArrayList 有哪些可用的方法,请参考这里:http ://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html (特别是我认为您对add() 方法)

于 2013-03-26T23:04:03.343 回答
0

首先,我建议为 Customer 对象设置值,然后执行 customer.add(Customer object)

就像是 :

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());

        }


    }

}
于 2013-03-26T23:17:45.283 回答
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

于 2013-03-26T23:27:24.627 回答