0
我一直在這一段時間,但似乎無法弄清楚如何將項目添加到ArrayList。我想補充grocItem(應該是從for循環的用戶輸入7個grocItems)插入grocList的ArrayList:從類ojects創建數組或ArrayList
public class ItemData{
public ItemData(String name, double cost, int priority){
Main.(ArrayList grocList).add(grocItem);
// Main.groclist.add(grocItem);
}
}
主類:
import java.util.*;
public class Main {
public static List<ItemData> itemData = new ArrayList<ItemData>();
public static void main(String[] args) {
int i=0;
//String name1;
//int priority1;
//double cost1;
String[] item = new String[7];
for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();
ItemData grocItem = new ItemData(name, cost, priority);
}
//How do I add grocItem to an Array list of other grocItems (6 grocItems from user input array item)
Main.itemData.add(groclist);
}
}