如何使用 List<Object> 将对象值设置为 Java 中的模型类?

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

How to set object value using List<Object> to a model class in java?

javaarraysarraylist

提问by Ke Vin

how do add each property value in my model using List? Here is what i do

如何使用 List 在我的模型中添加每个属性值?这是我所做的

here is my object : Item.java

这是我的对象: Item.java

public class Item {

    private String code;

    private String name;

    private Integer qty;

    // skip the getter setter
}

here is how i want to add the value from another class

这是我想从另一个类中添加值的方式

List<Item> sBarang = new ArrayList<Item>();
sBarang.add("");

How do I add each property value my Item.java?

如何将每个属性值添加到我的 Item.java?

What I can do is something like this :

我能做的是这样的:

Item mItem = new Item();
mItem .setCode("101");
mItem .setName("Hammer");
mItem .setQty(10);

采纳答案by Elliott Frisch

Unless I'm missing something you just need to add your mItemto your List. Like

除非我遗漏了一些东西,否则您只需mItem要将您的List. 喜欢

Item mItem = new Item(); // <-- instantiate a new Item.
mItem.setCode("101");
mItem.setName("Hammer");
mItem.setQty(10);
sBarang.add(mItem); // <-- add it to your List<Item>.

You could also create a new Itemconstructor that looks something like

您还可以创建一个Item看起来像的新构造函数

public Item(String code, String name, Integer qty) {
    this.code = code;
    this.name = name;
    this.qty = qty;
}

and then use a single line add like

然后使用单行添加

sBarang.add(new Item("101", "Hammer", 10)); 

回答by Patrick Chan

Make a constructor for your convenience.

为方便起见,制作一个构造函数。

public class Item {
    public Item(String code, String name, int qty){
        this.code=code;
        this.name=name;
        this.qty=qty;
    }
    private String code;

    private String name;

    private Integer qty;

    //skip the getter setter
}

Since then, you can add new "Item" object easily by

从那时起,您可以通过以下方式轻松添加新的“项目”对象

sBarang.add(new Item("101","Hammer",10));

回答by CorbinMc

sBarang.add("")is not going to work. Your trying to add a Stringto a list containing only Itemobjects.

sBarang.add("")不会工作。您尝试将 a 添加String到仅包含Item对象的列表中。

The second half of your post sounds like you're looking for a more efficient way to assign values to the fields of your Iteminstance. Do this by adding a constructor to your class. That will look like this:

您帖子的后半部分听起来像是在寻找一种更有效的方法来为Item实例的字段赋值。通过向类中添加构造函数来完成此操作。看起来像这样:

public class Item {

    public Item (String startCode, String startName, int startQty) {
        this.code = startCode;
        this.name = startName;
        this.qty = startQty;
    }
    ...
}

Initialize your item like this: Item myItem = new Item("101", "Hammer", 10);

像这样初始化你的项目: Item myItem = new Item("101", "Hammer", 10);

Add it to your list like this: sBarang.add(myItem);

像这样将其添加到您的列表中: sBarang.add(myItem);

Or use the one-liner: sBarang.add(new Item("101", "Hammer", 10));

或者使用单线: sBarang.add(new Item("101", "Hammer", 10));

回答by user11036750

 Item m= new Item(); 
    m.Set_Code("101");
    m.set_Name("Hammer");
    m.set_Qty(10);
    s_barang . add(m); 
    i need to store the next value but always i get the last value in the array how to clear the current data from model class object and push the next item into m...