将元素添加到对象数组

时间:2017-01-03 15:12:10

标签: java arrays arrayobject

我有一个名为EditorFor的类,其中一个属性是数组receipt,我有一个方法item[] items;。我的问题是如何将这些参数添加到数组addItem (string name , int quantity , double price)?如何检查数量是否大于0? 什么代码?我的问题清楚了吗? 她是我的代码:

items[]

5 个答案:

答案 0 :(得分:0)

您可以使用项目对象作为参数,而不是传输每个单独的属性。
如果使用数组,则应该有一个变量,用于存储数组中添加的最后一个元素的索引。

Item可以有一个构造函数和必需的getter:

public class Item{

  private String name;
  private int quantity;
  private double price;

 public Item(String name, int quantity, double price){
    this.name=name;
    this.quantity=quantity;
    this.price=price;
 }

  // required getters
}

public class Receipt{
   ...
   private int lastIndexUsed; // init to zero here
   ...
   private Item[] items = ...
   ...
    public boolean addItem(Item item){
      if(item.getQuantity() <= 0){
        System.out.println("Item wasnt added");
        return false;
      }
      else{
            items[lastIndexUsed++] = item;
        }
     }
}

使用方法:

Receipt receipt = new Receipt();
receipt.addItem(new Item("itemOne", 10, 50));
receipt.addItem(new Item("itemTwo", 20, 80));

答案 1 :(得分:0)

我找不到直接回答你问题的答案,所以在这里 我会使用ArrayList<Item>因为它具有您可以调用的.add()方法。如果需要,您可以稍后将其转换为数组.ToArray()(或类似内容):

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

ArrayList<Item> items = new ArrayList<>();
...

public boolean addItem(String name, int quantity, double price){
    if(quantity <= 0) {
        System.out.println("Item wasnt added");
        return false;  
    }
    else {
        //System.out.println("Item was added"); //if you want
        items.add(new Item(name, quantity, price)); //Create new Item with your parameters...  
        return true;
    }
}

不确定Java语法是否正确,IDE会帮助您。

答案 2 :(得分:0)

或者您可以使用ArrayList

public class Item{

  private String name;
  private int quantity;
  private double price;

  public Item(String name, int quantity, double price){
    this.name=name;
    this.quantity=quantity;
    this.price=price;
  }
}

public class Receipt{
   private ArrayList<Item> items = new ArrayList<Item>();

    public boolean addItem(Item item){
      if(item.getQuantity() <= 0){
        ...
        return false;
      }
      else{
            items.add(item);
        }
     }
}

答案 3 :(得分:0)

首先需要做两件事,确保你有一件物品。 []仅用于解除引用(所以说访问内存位置而不是对数组的引用)。您需要创建要添加的项目。你需要做的第二件事是确保有空间。您不能访问数组未保留的内存。

public class Receipt {
    private int nItems;
    private Item[] items;

    Receipt() {
        nItems = 0;
        items = new Item[10]; // Set initial size
    }

    /**
        Set initial size of array
    */
    Receipt(int initSize) {
        if (initSize <= 0) {
            throw new IllegalArgumentException("initSize must be larger than 0");
        }
        nItems = 0;
        items = new Item[initSize]; // Set initial size
    }

    public void addItem(Item item) {
        reserve();
        items[nItems] = item;
        nItems++; // Bad experiences of incrementing while dereferencing
    }

    /**
        Make sure there is enough space in items to add an ingredient
    */
    private void reserve() {
        if (items.length == nItems) {
            Item [] tmp = new Item[nItems*2]; // Double size if array is full.
            for (int i=0; i<nItems; i++) { // Copy the old elements to new array
                tmp[i] = items[i];
            }
            items = tmp; // Replace the old array with the new array.
        }
    }
}

答案 4 :(得分:0)

 public boolean addItem(String name, int quantity,double price)
 {
    if(quantity <= 0)
    {
     System.out.println("Item wasnt added");
     return false;
    }else{
         //you would have to create new object before adding it to array.
         item tempItem=new item(name,quantity,price);
         items[nItem++]=tempItem;
          }
 }