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

Posted

技术标签:

【中文标题】在循环中创建多个具有不同名称的对象以存储在数组列表中【英文标题】:Creating multiple objects with different names in a loop to store in an array list 【发布时间】:2013-11-26 12:43:32 【问题描述】:

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

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

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

public Customer(String customerName, double amount)

    String name=customerName;
    double sale=amount;

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



客户类别:

public class Customer 

private String name;
private double sale;

public Customer()




public Customer(String customerName, double amount)

    name=customerName;
    sale=amount;


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

    


【问题讨论】:

你的构造函数没有对传递的参数做任何有用的事情。 创建一个包含名称的字符串数组,当您循环添加对象时,在该数组中选择一个随机值。 什么意思?不是在创建 Customer 对象吗? 可以,但不设置任何字段,只设置局部变量。 我还有其他方法可以访问局部变量。 【参考方案1】:
ArrayList<Customer> custArr = new ArrayList<Customer>();
while(youWantToContinue) 
    //get a customerName
    //get an amount
    custArr.add(new Customer(customerName, amount);

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


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

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


将您的 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;
    

【讨论】:

谢谢!那么通过这样做,对象会没有名字吗?只有数组列表中的引用? 对...而不是myObject1myObject2myObject3,他们会是custArr.get(0)custArr.get(1)custArr.get(2),等等。你在代码中引用它......存储在对象中的数据并不关心你如何调用对象...... @nhgrif 抱歉,当我发布我的答案时没有答案 :) @m59 嗯,也许他们出现在您开始回答和发布回答之间。 @nhgrif 现在我收到错误“对于类型 Store,方法 add(Customer) 未定义”。我有3节课。商店、客户和商店测试。 Customer 类型的数组在 StoreTest 类中声明。 while 循环代码包含在 Store 类中并在 StoreTest 类中调用。那么我该如何摆脱这个错误呢?它出现在这一行:this.add(new Customer(customerName, amount));【参考方案2】:

您可以使用此代码...

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++;
        
    

【讨论】:

以上是关于在循环中创建多个具有不同名称的对象以存储在数组列表中的主要内容,如果未能解决你的问题,请参考以下文章

我正在尝试创建一个在循环遍历文本文件时使用变量值的类

如何在循环中创建多个具有不同名称的 tkinter 小部件?

创建/修改具有不同 QUOTED_IDENTIFIER 值的多个对象(在存储过程中)

如何创建具有从 csv 文件中的列表收集的唯一名称值的类的多个对象

编写循环以更改具有不同名称的 int 数组中的值

如何将具有多个对象的状态数组作为参数传递给graphql突变?