java 将数组对象添加到列表

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

adding array objects to a list

javaarrayslistobject

提问by Mariell

i'm new to java and I am having problems adding my array objects to my list. I have already checked the topics in this forum but can't find something similar to my problem.

我是 Java 新手,在将数组对象添加到列表时遇到问题。我已经检查了本论坛中的主题,但找不到与我的问题类似的内容。

I have already made a class called Item that will save information about an item to be sold, and it has 4 instance variables id, desc, cost, quantity. And then I have a Sales class which has my main method. Basically what I want to do is build an array of objects from my Item class and hard code my data(which I have already created, i'm not sure if I did it right though).

我已经创建了一个名为 Item 的类,它将保存有关待售商品的信息,它有 4 个实例变量 id、desc、cost、quantity。然后我有一个 Sales 类,其中包含我的主要方法。基本上我想要做的是从我的 Item 类构建一个对象数组并对我的数据进行硬编码(我已经创建了,但我不确定我是否做得对)。

I have created an ArrayList and what I want to do now is add the 5 objects from the array that I created into the list.

我已经创建了一个 ArrayList,我现在想要做的是将我创建的数组中的 5 个对象添加到列表中。

This is my Item class

这是我的项目类

public class Item {


    private String itemID;
    private String desc;
    private double cost;
    private int quantity;

    public Item() {
        itemID="";
        desc="";
        cost=0;
        quantity=0;
    }

    public Item(String id, String d, double c, int q) {
        itemID = id;
        desc = d;
        cost = c;
        quantity = q;

    }

    /**
     * @return the itemID
     */
    public String getItemID() {
        return itemID;
    }

    /**
     * @param itemID the itemID to set
     */
    public void setItemID(String itemID) {
        this.itemID = itemID;
    }

    /**
     * @return the desc
     */
    public String getDesc() {
        return desc;
    }

    /**
     * @param desc the desc to set
     */
    public void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * @return the cost
     */
    public double getCost() {
        return cost;
    }

    /**
     * @param cost the cost to set
     */
    public void setCost(double cost) {
        this.cost = cost;
    }

    /**
     * @return the quantity
     */
    public int getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Item [itemID=" + itemID + ", desc=" + desc + ", cost=" + cost
                + ", quantity=" + quantity + "]";
    }



}

And my Sales class:

还有我的销售课:

import java.util.*;

public class Sales {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int i;

        Item[] items = new Item[5];


        for(i = 0; i < items.length; i++)
        {
            items[i]= new Item(); // create array 
        }

        //hard-coded values of id, desc, cost, qty
        items[0].setItemID("PN250");
        items[1].setItemID("ER100");
        items[2].setItemID("PP150");
        items[3].setItemID("MK200");
        items[4].setItemID("PN300");

        items[0].setDesc("Pen");
        items[1].setDesc("Eraser");
        items[2].setDesc("Paper");
        items[3].setDesc("Marker");
        items[4].setDesc("Pen");

        items[0].setCost(1.50);
        items[1].setCost(1.25);
        items[2].setCost(3.75);
        items[3].setCost(2.50);
        items[4].setCost(2.25);

        items[0].setQuantity(0);
        items[1].setQuantity(175);
        items[2].setQuantity(310);
        items[3].setQuantity(75);
        items[4].setQuantity(450);

        double total = 0;
        for(Item d : items)
        {

            System.out.print(d.getItemID());
            System.out.print("\t" + d.getDesc());
            System.out.print("\t" + d.getCost());
            System.out.println("\t" + d.getQuantity());
        }

        List<Item> obj;
        obj = new ArrayList<Item>();

    }
}

回答by Cratylus

You could add the array into your list with the following:

您可以使用以下命令将数组添加到您的列表中:

obj.addAll(Arrays.asList(items));

回答by molyss

instead of creating an array at the beginning of your function, create an arrayList and add objects to it using the put method.

不要在函数的开头创建一个数组,而是创建一个 arrayList 并使用 put 方法向其中添加对象。

Also, you should think of making your objects immutables and pass their attributes to their constructor instead of calling the setter for each attribute

此外,您应该考虑使您的对象不可变并将它们的属性传递给它们的构造函数,而不是为每个属性调用 setter

回答by Reimeus

You can use:

您可以使用:

for (i = 0; i < items.length; i++)
{
    obj.add(items[i]);
}

but why not add the items as soon as they are created & populated?

但为什么不在创建和填充项目后立即添加项目?