配列リストを使い始めたばかりなので、ご容赦ください。アイテム名、アイテム番号、在庫数、アイテムの価格を含むインデックス 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 エントリになりますか? 私はこれをやろうとしましたが、好きではありません。また、各タイプの間にコンマを入れようとしましたが、それもうまくいかないようです。私のディスプレイはこの状況に適していないのでしょうか? どんな助けでも素晴らしいでしょう。これは実際には、各行がこれらの特性を持つ多次元型配列用です。