使用 get 方法中的值添加到 ArrayList 的最合適方法是什么。
我知道下面的代碼行是一個錯誤,我無法弄清楚其中有什么問題。
System.out.println("List of Foods");
List<String> foodList = new ArrayList<>();
foodList.addAll(getId(), getDescription(), getIngredients(), getSellingPrice());
System.out.println(foodList);
錯誤資訊:
Multiple markers at this line
- Occurrence of 'addAll'
- The method addAll(int, Collection<? extends String>) in the type List<String> is not applicable
for the arguments (int, String, List<String>, double)
- 1 changed line
背后的想法是能夠在 main 方法中檢索這些用戶輸入并使用以下代碼顯示它
System.out.println("---------------------------------------------------------------------------");
System.out.printf("%5s s s 0s", "FOOD ID", "DESCRIPTION", "SELLING PRICE", "INGREDIENTS" "\n");
System.out.println("---------------------------------------------------------------------------");
//iterates over the list
for (Food food : foods) {
System.out.format("%5s s s 0s", food.getId(), food.getDescription(), food.getSellingPrice(), food.getIngredients());
System.out.println();
}
System.out.println("---------------------------------------------------------------------------");
任何幫助都感激不盡。
get() 方法定義:
public int getId() {
return id;
}
public String getDescription() {
return description;
}
public double getSellingPrice() {
return sellingPrice;
}
public List<String> getIngredients() {
return ingredients;
}
我更改了以下代碼行:
System.out.println("List of Foods");
List<String> foodList = new ArrayList<>();
foodList.add(getId());
foodList.add(getDescription());
foodList.addAll(getIngredients());
foodList.add(getSellingPrice());
System.out.println(foodList);
我已經改變了現有的 Food 類:
public class Food {
private String id;
private String description;
private String sellingPrice;
private List<String> ingredients;
Scanner sc = new Scanner(System.in);
public Food(String id, String description, String sellingPrice, List<String> ingredients) {
super();
this.id = id;
this.description = description;
this.sellingPrice = sellingPrice;
this.ingredients = ingredients;
經過一些調整后,以下代碼出現了一個新問題:
case 2:
System.out.println("List of Foods");
List<Object> foodList = new ArrayList<>();
foodList.add(getId());
foodList.add(getId());
foodList.add(getDescription());
foodList.addAll(getIngredients());
foodList.add(getSellingPrice());
System.out.println(foodList);
List<Object> food1 = new ArrayList<>();
food1.add(foodList);
List<Object> food2 = new ArrayList<>();
food2.add(foodList);
List<Object> food3 = new ArrayList<>();
food3.add(foodList);
System.out.println("---------------------------------------------------------------------------");
System.out.printf("%5s s s 1s", "FOOD ID", "DESCRIPTION", "SELLING PRICE(Rs)", "INGREDIENTS" "\n");
System.out.println("---------------------------------------------------------------------------");
for (Object food : foodList) {
System.out.format("%5s s s 0s", getId(), getDescription(), getSellingPrice(), getIngredients());
System.out.println();
}
break;
控制臺輸出
1 - 用于添加食物 2- 用于展示
uj5u.com熱心網友回復:
假設 getId()、getDescription()、getIngredients()、getSellingPrice() 方法正在讀取用戶輸入。
您將需要創建一個 Food 類來保存這些值,并為顯示邏輯創建一個 Food 的 ArrayList。
偽代碼
public class Food{
private String id;
private String description;
private String ingredients;
private double sellingPrice;
// Constructor, Getter, Setter
}
List<Food> foodList = new ArrayList<>();
String id = getId()
String description = getDescription()
String ingredients = getIngredients()
double sellingPrice = getSellingPrice()
Food food = new Food(id, description, ingredients, sellingPrice)
foodList.add(food);
uj5u.com熱心網友回復:
你腦子里混了很多東西,讓我們澄清一些
定義一個Food類,它有get方法,它不需要set那些
class Food {
private int id;
private String description;
private String sellingPrice;
private List<String> ingredients;
public Food(int id, String description, String sellingPrice, List<String> ingredients) {
this.id = id;
this.description = description;
this.sellingPrice = sellingPrice;
this.ingredients = ingredients;
}
public int getId() {
return id;
}
public void setId(int id) { // JUST FOR EXAMPLE, you don't need the set method
this.id = id;
}
public String getDescription() {
return description;
}
public String getSellingPrice() {
return sellingPrice;
}
public List<String> getIngredients() {
return ingredients;
}
@Override
public String toString() {
return String.format("%5s s s 0s", id, description, sellingPrice, ingredients);
}
}
然后是一個主類,不管它的名字是什么,自定義方法來詢問用戶輸入
class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("How many food do you want to register ?");
int count = Integer.parseInt(sc.nextLine());
List<Food> foods = new ArrayList<>();
for (int i = 0; i < count; i ) {
foods.add(new Food(generateId(), generateDescription(), generateSellingPrice(), generateIngredients()));
}
System.out.println("---------------------------------------------------------------------------");
System.out.printf("%5s s s 1s", "FOOD ID", "DESCRIPTION", "SELLING PRICE(Rs)", "INGREDIENTS" "\n");
System.out.println("---------------------------------------------------------------------------");
for (Food food : foods) {
System.out.println(food);
// System.out.format("%s", food.getId()); // if toString wasn't implemented you'd that
}
}
private static int generateId() {
System.out.println("Input food ID");
return Integer.parseInt(sc.nextLine());
}
private static String generateDescription() {
System.out.println("Input food name");
return sc.nextLine();
}
private static String generateSellingPrice() {
System.out.println("Input food Price");
return sc.nextLine();
}
private static List<String> generateIngredients() {
List<String> ingredients = new ArrayList<String>();
System.out.println("Enter the number of Ingredients to add: ");
int ingredientCount = Integer.parseInt(sc.nextLine());
for (int i = 0; i < ingredientCount; i ) {
System.out.println("Enter an ingredient: ");
ingredients.add(sc.nextLine());
}
return ingredients;
}
}
uj5u.com熱心網友回復:
確保添加行。
List<String>- 必須包含字串。轉換為字串。我們將您放入 List<> 的方法回傳什么型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/493750.html
上一篇:如何更改R中串列的格式?
下一篇:在導航視圖中為可搜索串列創建視圖
