Java 在循环中创建多个具有不同名称的对象以存储在数组列表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19961844/
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-08-12 21:51:34  来源:igfitidea点击:

Creating multiple objects with different names in a loop to store in an array list

javaobjectarraylistwhile-loop

提问by user1953907

I am trying to create mutliple objects of a type of class I made. I then want to transfer these values into the array list. How can I create objects using a while loop that have different names. For example here is my code now, but it would only make an object of one name.

我正在尝试创建我创建的一类类的多个对象。然后我想将这些值传输到数组列表中。如何使用具有不同名称的 while 循环创建对象。例如,这是我现在的代码,但它只会创建一个名称的对象。

Customer cust = new Customer("bob", 20.0);

and my constructor if you want to see:

和我的构造函数,如果你想看:

public Customer(String customerName, double amount)
{
    String name=customerName;
    double sale=amount;
}

StoreTest class(with main method):

StoreTest 类(使用 main 方法):

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

public class StoreTest {

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

public static void main (String[] args)
{
        double sale=1.0; //so the loop goes the first time
        //switch to dowhile
        Scanner input = new Scanner(System.in);

        System.out.println("If at anytime you wish to exit" +
                ", please press 0 when asked to give " +
                "sale amount.");
        while(sale!=0)
        {
            System.out.println("Please enter the " +
                    "customer's name.");
            String theirName = input.nextLine();

            System.out.println("Please enter the " +
                    "the amount of the sale.");
            double theirSale = input.nextDouble();

            store.addSale(theirName, theirSale);
        }
        store.nameOfBestCustomer();
}

}

Customer class:

客户类别:

public class Customer {

private String name;
private double sale;

public Customer()
{

}

public Customer(String customerName, double amount)
{
    name=customerName;
    sale=amount;
}
}

Store class(has methods for messing with arraylist:

Store 类(有处理数组列表的方法:

import java.util.ArrayList;


public class Store {

//creates Customer object and adds it to the array list
public void addSale(String customerName, double amount)
{
    this.add(new Customer(customerName, amount));
}

//displays name of the customer with the highest sale
public String nameOfBestCustomer()
{
    for(int i=0; i<this.size(); i++)
    {

    }
}
}

采纳答案by nhgrif

ArrayList<Customer> custArr = new ArrayList<Customer>();
while(youWantToContinue) {
    //get a customerName
    //get an amount
    custArr.add(new Customer(customerName, amount);
}

For this to work... you'll have to fix your constructor...

为了让它工作......你必须修复你的构造函数......



Assuming your Customerclass has variables called nameand sale, your constructor should look like this:

假设您的Customer类具有名为nameand 的变量sale,您的构造函数应如下所示:

public Customer(String customerName, double amount) {
    name = customerName;
    sale = amount;
}


Change your Storeclass to something more like this:

将您的Store课程更改为更像这样的内容:

public class Store {

    private ArrayList<Customer> custArr;

    public new Store() {
        custArr = new ArrayList<Customer>();
    }

    public void addSale(String customerName, double amount) {
        custArr.add(new Customer(customerName, amount));
    }

    public Customer getSaleAtIndex(int index) {
        return custArr.get(index);
    }

    //or if you want the entire ArrayList:
    public ArrayList getCustArr() {
        return custArr;
    }
}

回答by programsji.com

You can use this code...

您可以使用此代码...

public class Main {

    public static void main(String args[]) {
        String[] names = {"First", "Second", "Third"};//You Can Add More Names
        double[] amount = {20.0, 30.0, 40.0};//You Can Add More Amount
        List<Customer> customers = new ArrayList<Customer>();
        int i = 0;
        while (i < names.length) {
            customers.add(new Customer(names[i], amount[i]));
            i++;
        }
    }
}