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

Java 在循环中创建多个名称不同的对象以存储在数组列表中,java,object,arraylist,while-loop,Java,Object,Arraylist,While Loop,我正在尝试创建我创建的类类型的多个对象。然后我想将这些值转移到数组列表中。如何使用具有不同名称的while循环创建对象。例如,这里是我现在的代码,但它只生成一个名称的对象 Customer cust = new Customer("bob", 20.0); 和我的构造函数,如果你想看到: public Customer(String customerName, double amount) { String name=customerName; double sale=amou

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

Customer cust = new Customer("bob", 20.0);
和我的构造函数,如果你想看到:

public Customer(String customerName, double amount)
{
    String name=customerName;
    double sale=amount;
}
存储测试类(带主方法):

存储类(具有处理arraylist的方法:

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++)
    {

    }
}
}
import java.util.ArrayList;
公共类商店{
//创建客户对象并将其添加到数组列表中
公共作废addSale(字符串customerName,双倍金额)
{
添加(新客户(客户名称、金额));
}
//显示销售额最高的客户的名称
BESTCUSTOMER()的公共字符串名称
{

对于(inti=0;i您可以使用此代码

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++;
        }
    }
}
公共类主{
公共静态void main(字符串参数[]){
字符串[]名称={“第一”、“第二”、“第三”};//您可以添加更多名称
double[]金额={20.0,30.0,40.0};//您可以添加更多金额
列出客户=新建ArrayList();
int i=0;
while(i

存储
类更改为类似以下内容:

public Customer(String customerName, double amount) {
    name = customerName;
    sale = amount;
}
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;
    }
}
公共类存储{
私人ArrayList客户;
公共新店(){
custArr=newarraylist();
}
公共作废addSale(字符串customerName,双倍金额){
客户添加(新客户(客户名称、金额));
}
公共客户GetSaleAtinex(内部索引){
返回客户获取(索引);
}
//或者,如果需要整个ArrayList:
公共阵列列表getCustArr(){
归还客户;
}
}

构造函数对传递的参数没有做任何有用的操作。创建一个包含名称的字符串数组,当您循环添加对象时,在该数组中选择一个随机值。这是什么意思?它不是在创建客户对象吗?当然,但不设置任何字段,只设置局部变量。我有其他方法可以访问局部变量变量。谢谢!这样做会使对象没有名称?只有数组列表中的引用?对…而不是
myObject1
myObject2
myObject3
,它们将是
custArr.get(0)
custArr.get(1)
custArr.get(2)
,等等。但名称只是你在代码中引用它的方式…对象中存储的数据不关心你如何调用对象…@nhgrif抱歉,我发布我的时没有答案:)@m59 Mmm,可能它们出现在你启动答案和发布答案之间。@nhgrif现在我得到错误“方法添加”(Customer)对于类型Store.是未定义的。我有3个类。Store、Customer和StoreTest。Customer类型的数组在StoreTest类中声明。while循环代码包含在Store类中,并在StoreTest类中调用。那么如何消除此错误呢?它出现在这一行:
this.add(new Customer)(客户名称、金额);
ArrayList<Customer> custArr = new ArrayList<Customer>();
while(youWantToContinue) {
    //get a customerName
    //get an amount
    custArr.add(new Customer(customerName, amount);
}
public Customer(String customerName, double amount) {
    name = customerName;
    sale = amount;
}
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;
    }
}