简体   繁体   English

如何添加一个数组来存储项目?

[英]How to add an array to store items?

I am new to java and am writing a program for class that I have to modify so that it can handle multiple items and I need to use an array to store the items. 我是java的新手,正在为我的类编写程序,我必须对其进行修改,以便它可以处理多个项目,并且我需要使用数组来存储项目。 This is what I have so far. 到目前为止,这就是我所拥有的。

public class ProductDVD
    {
    private String productName;
    private int productID;
    private long unitsInStock;
    private float unitPrice;

public void setName( String productName )
    {
    this.productName = productName;
    }

public void setProductID( int productID )
    {
    this.productID = productID;
    }

public void setUnitsInStock( long unitsInStock )
    {
    this.unitsInStock = unitsInStock;
    }

public void setUnitPrice( float unitPrice )
    {
    this.unitPrice = unitPrice;
    }

    public String getName()
    {
    return productName;
    }

    public int getProductID()
    {
    return productID;
    }

    public long getUnitsInStock()
    {
    return unitsInStock;
    }

    public float getUnitPrice()
    {
    return unitPrice;
    }

    public float getInventoryValue()
    {
    return( unitsInStock * unitPrice );
    }
    }


import java.util.Scanner;

    public class DVDinventory
    {
    public static void main( String args [] )
    {
    Scanner input = new Scanner( System.in );

    ProductDVD dvd = new ProductDVD();

    System.out.print( "Enter a DVD Title: " );
    dvd.setName( input.nextLine() );

    System.out.print( "Enter the Product ID: " );
    dvd.setProductID( input.nextInt()); 

    System.out.print( "Enter the Product's Unit Price: " );
    dvd.setUnitPrice( input.nextFloat() );

    System.out.print( "Enter the Number of Units in Stock: " );
    dvd.setUnitsInStock( input.nextLong() );

    input.nextLine();

    System.out.println( "Name:          " + dvd.getName() );
    System.out.println( "ID:            " + dvd.getProductID() );
    System.out.println( "Unit Price:        " + dvd.getUnitPrice() );
    System.out.println( "Units in Stock:        " + dvd.getUnitsInStock() );
    System.out.println( "Inventory Value:   " + dvd.getInventoryValue() );
    }
    }

The question is how do I create an array that can hold both string and int elements? 问题是如何创建既可以容纳字符串元素又可以容纳int元素的数组? I need the array to hold the DVD name, id, unit price, units in stock, and total inventory value. 我需要一个阵列来保存DVD名称,id,单价,库存单位和总库存值。

Using a java.util.Collection would make more sense, but if you HAVE to use an array for your homework: 使用java.util.Collection会更有意义,但是如果您必须使用数组进行作业:

private static int INVENTORY_SIZE = 42; //that's what you mean by total inventory value, right?

ProductDVD[] dvds = new ProductDVD[INVENTORY_SIZE];

Scanner input = new Scanner( System.in );

for (int i = 0; i < INVENTORY_SIZE; i++) {
    dvds[i] = new ProductDVD();  
    //your code to read in data here...
}

将“ 收集列表”作为起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM