使用ArrayList在java中添加新对象?

时间:2013-03-26 23:01:22

标签: java arraylist

我无法理解ArrayList。我正在编写一个程序,使用3个类Customer,Video和Invoice。在下面的代码中,我创建了一个新客户来添加到设置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 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

首先,我建议将值设置为Customer对象,然后执行customer.add(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());

        }


    }

}

答案 2 :(得分:0)

首先,你应该重命名

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

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

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

接下来,这是语法错误:

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

逗号用于分隔参数,例如max(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');


}

答案 3 :(得分: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