Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在对象数组中添加和打印?_Java - Fatal编程技术网

Java 如何在对象数组中添加和打印?

Java 如何在对象数组中添加和打印?,java,Java,我在做书店系统。用户必须选择是否添加、删除等。如果他选择添加,那么他将写入所有书籍属性,这些属性稍后将保存在项目数组“项目数组是一个数组对象”中 这是我的密码 public class userChoices { Items[] items = new Items[200]; AddItem add = new AddItem(); Scanner s = new Scanner(System.in); public void add(){ b

我在做书店系统。用户必须选择是否添加、删除等。如果他选择添加,那么他将写入所有书籍属性,这些属性稍后将保存在项目数组“项目数组是一个数组对象”中 这是我的密码

public class userChoices {

    Items[] items = new Items[200];
    AddItem add = new AddItem();
    Scanner s = new Scanner(System.in);

    public void add(){
        boolean invalidInput;
        int q = -1;
        do {        
            try {        
                invalidInput = false;
        System.out.println("How many book(s) you want to add?");
            q = s.nextInt();
            for(int i = 0; i < q; i++){
                add.getCode();
                add.getQuantity();
                add.getCostPrice();
                add.getDescription();
                add.getDiscount();
                add.getSellingPrice();
                add.getStatus();
                for(int r = 0; r < items.length; r++){
                    if(items[r] != null){
                        items[r] = new Items(add.getCode(), add.getDescription(), add.getQuantity(),
                                add.getCostPrice(), add.getSellingPrice(), add.getStatus(), add.getDiscount());
                        continue;
                    }
                }
            }

            } catch (InputMismatchException e) {
        System.out.println("Please enter a valid integer.");
                s.next();
        invalidInput = true;  // This is what will get the program to loop back
            }
    } while (invalidInput);
    }

this is my Items class
    public class Items {
    private int code, quantity;
    private String description;
    private double costPrice, sellingPrice;
    String status, discount;

    public Items(){
        this.code = 1111;
        this.quantity = 1;
        this.description = "Action";
        this.costPrice = 12.00;
        this.sellingPrice = 16.00;
        this.discount = "5%";
        this.status = "Unvailable";

    }

    public Items(int code, String description, int quantity,
            double costPrice, double sellingPrice, String status, String discount){
        this.code = code;
        this.description = description;
        this.quantity = quantity;
        this.costPrice = costPrice;
        this.sellingPrice = sellingPrice;
        this.status = status;
        this.discount = discount;
    }

    public void setCode(int code){
        this.code = code;
    }

    public void setQuantity(int quantity){
        this.quantity = quantity;
    }

    public void setDescription(String description){
        this.description = description;
    }

    public void setcostPrice(double costPrice){
        this.costPrice = costPrice;
    }

    public void setsellingPrice(double sellingPrice){
        this.sellingPrice = sellingPrice;
    }

    public void setStatus(String status){
        this.status = status;
    }

    public void setDiscount(String discount){
        this.discount = discount;
    }

    public int getCode(int code){
        this.code = code;
        return this.code;
    }

    public int getQuantity(int quantity){
        this.quantity = quantity;
        return this.quantity;
    }

    public String getDescription(String description){
        this.description = description;
        return this.description;
    }

    public double getcostPrice(double costPrice){
        this.costPrice = costPrice;
        return this.costPrice;
    }

    public double getsellingPrice(double sellingPrice){
        this.sellingPrice = sellingPrice;
        return this.sellingPrice;
    }

    public String getStatus(String status){
        this.status = status;
        return this.status;
    }

    public String getDiscount(String discount){
        this.discount = discount;
        return this.discount;
    }

    public String toString(){
        return ("code : " + this.code + "\nQuantity : " + this.quantity +
                "\nDescription : " + this.description + "\nCost price : " + this.costPrice
                + "\nSelling price : " + this.sellingPrice + "\nstatus : " + this.status
                + "\ndiscount : " + this.discount);
    }


}
公共类用户选择{
项目[]项=新项目[200];
AddItem add=新的AddItem();
扫描仪s=新的扫描仪(System.in);
公共无效添加(){
布尔输入;
int q=-1;
做{
试试{
无效输入=假;
System.out.println(“您要添加多少本书?”);
q=s.nextInt();
对于(int i=0;i

当我在那里添加项目[0]后打印项目时,它会显示“null”

您的错误在这一行:

                    if(items[r] != null){
创建数组时,由于它是一个对象引用数组,所以它的所有元素都为null。因此,此条件导致它永远不会将项插入数组中


无论如何,你不应该在那里有一个循环。你应该保留数组中下一个空位置的索引,并使用它将项目放入数组中,然后递增(向其中添加一个)。

除了其他人指出的
if(items[r]!=null){
之外,我相信这段代码中的continue语句是错误的:

for(int r = 0; r < items.length; r++){
    if(items[r] != null){
        items[r] = new Items(add.getCode(), add.getDescription(), add.getQuantity(),
                add.getCostPrice(), add.getSellingPrice(), add.getStatus(), add.getDiscount());
        continue;
    }
}
这个“getter”本质上是一个setter,它返回您刚刚设置的值,这不是getter应该做的

public double getcostPrice(){
    return this.costPrice;
}

这就是getter的工作原理。

如果(items[r]!=null),请只发布相关代码
if(items[r]!=null)
如果数组为空,则不会添加任何书籍。应该是
if(items[r]==null)
他为什么要跟踪上次插入的索引?
if(items[r]==null)
工作正常。@Tom,那里的循环是多余的。对于每个项目,从数组开始直到找到一个空值,都不需要运行nead。你已经知道下一个空值的位置-上一轮使用的数组元素之后的数组元素。没有必要首先使用数组:D。a
Collection
将好得多。你可以在回答中推荐这一点。(跟踪下一个索引的问题可能是,这个索引已经被另一种方法填充了)@Tom我认为他在研究中没有收集到那么多。好吧,这是一个很好的观点来介绍他们:D。但是,不管怎样,他的问题得到了回答。
for(int i = 0; i < q; i++){
public double getcostPrice(double costPrice){
    this.costPrice = costPrice;
    return this.costPrice;
}
public double getcostPrice(){
    return this.costPrice;
}