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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15649016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:25:03  来源:igfitidea点击:

Using an ArrayList to add new objects in java?

javaarraylist

提问by user2213630

I'm having trouble understanding an ArrayList. I am writing a program using 3 classes Customer, Video, and Invoice. In the code below I am creating a new customer to add to set ArrayList but I feel I am trying to treat it as if it were an array. I want the user to be able to add another customer object and use a counter "i" and run through the series of questions to add to that customer object. i realize some of this is quite a mess.

我在理解 ArrayList 时遇到问题。我正在使用 3 个类 Customer、Video 和 Invoice 编写程序。在下面的代码中,我正在创建一个新客户以添加到 set 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');


    }

}

回答by dspyz

Java does not support operator overloading and classes from the Java standard library are just normal classes.

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

This means that the only time it makes sense to use [] is with arrays (and not ArrayList which is a class in the Collections Framework). If you need to know what methods ArrayList has available, please refer here: http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html(specifically I think you're interested in the add() method)

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

回答by Laura Konda

First I would recommend to set values to the Customer object and then do customer.add(Customer object)

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

Something like :

就像是 :

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

        }


    }

}

回答by kajacx

First, you should rename

首先,您应该重命名

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

to

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

so everybody (including author) knows that it is some container with customers.

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

Next, this is an syntax error:

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

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

Comma is used to separate arguments, like max(2, 4, 5). You are adding a number, but this container can have only customers inside, also arraylist automatically increments indexes when adding so dont worry about some i. So if you want to add a customer to ArrayList customers, you first need to create one. Something that could work...

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


}

回答by Nathan Power

Further to the above answers, try the following:

除了上述答案,请尝试以下操作:

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

}

Note that the add() method adds an element to the next index in the ArrayList, you do not need to specify an index with square brackets [].

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

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

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