Java 将元素添加到对象数组中

Java 将元素添加到对象数组中,java,arrays,arrayobject,Java,Arrays,Arrayobject,我有一个名为receipt的类,其中一个属性是数组item[]items

我有一个名为
receipt
的类,其中一个属性是数组
item[]itemsaddItem(字符串名、整数数量、双倍价格)
。我的问题是如何将这些参数添加到数组
项[]
?如何检查数量是否超过0?
密码是什么?我的问题清楚吗?
这是我的密码:

public boolean addItem(String name, int quantity,double price){
if(quantity <= 0){
    System.out.println("Item wasnt added");
    return false;}
    else{
        item [nItem++];//here is the problem
    }
    }
public boolean addItem(字符串名称、整数数量、双倍价格){

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

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.
        }
    }
}
项可以具有构造函数和所需的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;
        }
     }
}

我找不到直接回答您问题的答案,所以请点击此处。
我会使用
ArrayList
,因为它有一个可以调用的
.add()
方法。如果需要,您可以稍后使用
.ToArray()
(或类似的方法)将其转换为数组:

。。。
导入java.util.ArrayList;
...
ArrayList items=新建ArrayList();
...
公共布尔附加项(字符串名称、整数数量、双倍价格){

如果(数量或您可以使用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);
        }
     }
}
公共类项目{
私有字符串名称;
私人整数数量;
私人双价;
公共项目(字符串名称、整数数量、双倍价格){
this.name=name;
这个。数量=数量;
这个。价格=价格;
}
}
公共类收据{
private ArrayList items=new ArrayList();
公共布尔添加项(项){

如果(item.getQuantity()您首先需要做两件事,以确保您拥有一个项。
[]
仅用于解引用(也就是说,访问内存位置,而不是对数组的引用)。您需要创建一个要添加的项。您需要做的第二件事是确保有空间。不允许您访问数组未保留的内存

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.
        }
    }
}
公共类收据{
私人机构;
私人物品[]项;
收据(){
nItems=0;
items=新项目[10];//设置初始大小
}
/**
设置数组的初始大小
*/
收据(int initSize){
if(initSize
public boolean addItem(字符串名、整数数量、双倍价格)
{

如果(quantity
newitem(variable1、variable2和_so_on)
可能会起到神奇的作用。我假设您需要创建
Item
类的新实例,然后使用赋值将其添加到数组中。这两个东西都丢失了。可能与我创建的重复。我创建了一个Item类。具有3个属性(字符串名称、整数数量、双倍价格)…set和get..除了上面的注释外,您可能还希望为数组中的新项保留空间。
if(item.length
…In addItem()当此条件为true时,您可以引发自定义异常
lastinexused==MAX_SIZE-1
。我是stackoverflow新手,仍在学习中。下次我会尽最大努力。按编辑按钮并正确操作并不难。抱歉,但是-1
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.
        }
    }
}
 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;
          }
 }