0

配列リストを使い始めたばかりなので、ご容赦ください。アイテム名、アイテム番号、在庫数、アイテムの価格を含むインデックス 0 のエントリを作成しようとしています。InventoryItem クラスを使用するように配列リストを作成したと思いますが、作成したかどうかはよくわかりません。これが私の InventoryItem クラスです。

class InventoryItem { //Delcare variables below

String ItemName;
int ItemNumber;
int InStock;
double UnitPrice;
double Value;

public InventoryItem(String ItemName, int ItemNumber, int InStock, double UnitPrice) { //declare variables for this class
    this.ItemName = ItemName;
    this.ItemNumber = ItemNumber;
    this.InStock = InStock;
    this.UnitPrice = UnitPrice;
    this.Value = UnitPrice * InStock; //calculate value of inventory

}

public void output() {
    System.out.println("Item Name = " + ItemName); //print out the item name
    System.out.println("Item Number = " + ItemNumber); //print out the item number
    System.out.println("In Stock = " + InStock); //print out how many of the item are in stock
    System.out.println("Item Price = $" + UnitPrice); //print out the price per item
    System.out.println("Value of inventory = $" + Value); //calculate how much the inventory is worth for this one item

}

ご覧のとおり、このクラスには文字列、整数、および double がリストされています。ここに示すように、配列リストを作成しました。(私はそれらをすべて0スポットに挿入しようとしました)

package inventory;

import java.util.ArrayList;

パブリック クラス インベントリ {

public static void main(String[] args) {
    ArrayList InventoryItem = new ArrayList();
    InventoryItem.add(0, "Pencil ");
    InventoryItem.add(0, "123456789");
    InventoryItem.add(0, "1");
    InventoryItem.add(0, "1.25");

    for (int i = 0; i< InventoryItem.size(); i++)
        System.out.printf("%s", InventoryItem.get(i));
    System.out.println();

私の問題は、このリストが 1 行で入力できるようにしたかったことです。次のようなことができますか

ArrayList<String int int double> Stock = new ArrayList<String int int double>();
InventoryItem.add(0, "Pencil", 123456789, 1, 1.25);

これで意図したとおりに 0 エントリになりますか? 私はこれをやろうとしましたが、好きではありません。また、各タイプの間にコンマを入れようとしましたが、それもうまくいかないようです。私のディスプレイはこの状況に適していないのでしょうか? どんな助けでも素晴らしいでしょう。これは実際には、各行がこれらの特性を持つ多次元型配列用です。

4

3 に答える 3

1

これを試して

ArrayList<InventoryItem> inventory = new ArrayList<InventoryItem>();
InventoryItem in_1 = new InventoryItem(0, "Pencil", 123456789, 1, 1.25);
inventory.add(in_1);

または一行で

inventory.add(new InventoryItem(0, "Pencil", 123456789, 1, 1.25));
于 2013-11-12T05:24:33.757 に答える
0

InventoryItem 呼び出しが既にあるので、そのオブジェクトを作成し、値をオブジェクトに設定し、このオブジェクトを ArrayList に追加します。

   InventoryItem item = new InventoryItem("Pencil", 123456789, 1, 1.25);
   ArrayList<InventoryItem> itemList = new ArrayList<InventoryItem>();
   itemList.add(item);

取得中に、以前に設定したすべてのデータを含む項目オブジェクトを取得します

于 2013-11-12T05:25:36.767 に答える
0

ご協力いただきありがとうございます。それがなければ、今の自分にたどり着くことができませんでした。コードを次のように変更しました。

public static void main(String[] args) {

    ArrayList<inventoryItem> inventory = new ArrayList<inventoryItem>();
    inventory.add(new inventoryItem("Pencil", 1111, 10, .25));
    inventory.add(new inventoryItem("Pen", 9876, 2222, .99));
    inventory.add(new inventoryItem("Canned Air", 3333, 5, 2.00));
    inventory.add(new inventoryItem("Notebook", 4444, 10, 2.50));
    inventory.add(new inventoryItem("Staples", 5555, 5, 1.00));
    inventory.add(new inventoryItem("Paper Clips", 6666, 10000, .05));



    double total = 0;

    for (int i = 0; i < inventory.size(); i++) {
        inventory.get(i).output();

    }
    inventoryTotal(inventory);


}

public static void inventoryTotal(ArrayList<inventoryItem> inventory) {
    double total = 0;

    for (int i = 0; i < inventory.size(); i++) {
        total = total + inventory.get(i).value;
    }
    System.out.printf("Total value of inventory $%.2f\n", + total);

これにより、inventory.add を使用して arraylist に追加し、それを割り当てることができます。次にやらなければならないことは、項目をアルファベット順にソートすることですが、それはできると思います。

あなたのご親切に感謝します!

于 2013-11-13T01:41:48.987 に答える