如何使用get方法返回值正确向ArrayList添加元素并遍历展示
使用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
报错原因:
List的addAll方法仅支持两个重载形式:addAll(Collection<? extends E> c):传入1个集合参数,将集合内所有元素添加到当前列表addAll(int index, Collection<? extends E> c):传入索引位置+1个集合参数,在指定位置插入集合所有元素
你传入4个不同类型参数的写法完全不符合方法签名要求。
- 你定义的
List<String>只能存储字符串类型值,但getId()返回int、getSellingPrice()返回double,本身类型就不匹配。
你项目中原有的get方法定义是合理的,不需要修改返回值类型:
public int getId() { return id; } public String getDescription() { return description; } public double getSellingPrice() { return sellingPrice; } public List<String> getIngredients() { return ingredients; }
之前调整方案的问题
你先后尝试的两种调整方向都存在逻辑偏差:
- 强行把Food类的
id、sellingPrice属性改为String类型属于削足适履,整型ID、浮点型售价本身符合业务语义,不需要为了适配列表强行修改属性类型。 - 后续改用
List<Object>存储、重复add id、嵌套创建food1/food2/food3列表的逻辑完全偏离需求:你需要存储的是多个食品对象,不是把单个食品的属性拆碎后混在无类型的Object列表里,更不需要嵌套列表结构。
最合理实现方式
核心思路:直接定义List<Food>类型的列表存储食品实例,不要拆分单个对象的属性,不要用Object类型丢失泛型检查,完全贴合你预期的格式化输出逻辑。
1. 保持Food类的合理类型定义
回滚你对Food类属性类型的修改,保留符合业务的类型声明:
public class Food { private int id; private String description; private double sellingPrice; private List<String> ingredients; public Food(int id, String description, double sellingPrice, List<String> ingredients) { this.id = id; this.description = description; this.sellingPrice = sellingPrice; this.ingredients = ingredients; } // 保留原有get方法,不需要修改返回值 public int getId() { return id; } public String getDescription() { return description; } public double getSellingPrice() { return sellingPrice; } public List<String> getIngredients() { return ingredients; } }
2. 维护全局食品列表
在菜单逻辑外部定义List<Food>类型的列表统一存储所有食品,选项1新增食品时,收集完用户输入直接构造Food对象添加到列表即可:
// 菜单循环外定义,全局维护所有食品数据 List<Food> foods = new ArrayList<>(); // 选项1新增食品逻辑示例(收集完用户输入后执行): // foods.add(new Food(用户输入的int类型id, 用户输入的描述, 用户输入的double类型售价, 用户输入的配料列表));
3. 选项2展示列表的正确实现
不需要临时拆分属性拼列表,直接遍历foods中的Food实例取值输出即可,配料列表可以提前转成逗号分隔的字符串优化展示效果:
case 2: System.out.println("List of Foods"); System.out.println("---------------------------------------------------------------------------"); System.out.printf("%5s %15s %20s %31s", "FOOD ID", "DESCRIPTION", "SELLING PRICE(Rs)", "INGREDIENTS\n"); System.out.println("---------------------------------------------------------------------------"); for (Food food : foods) { // 将配料列表转为逗号分隔的字符串,优化展示格式 String ingredientDisplay = String.join(", ", food.getIngredients()); System.out.format("%5s %17s %20s %30s", food.getId(), food.getDescription(), food.getSellingPrice(), ingredientDisplay); System.out.println(); } System.out.println("---------------------------------------------------------------------------"); break;
该方案的优势:
- 符合面向对象设计原则,属性类型匹配业务语义,没有冗余的类型转换
- 泛型明确为
Food类型,编译期即可发现类型错误,避免运行时异常 - 逻辑清晰,不需要冗余的嵌套列表、重复添加属性的操作,可维护性强
- 和你最初设计的格式化输出逻辑完全匹配
内容的提问来源于stack exchange,提问作者raj_b
相关产品推荐
相关产品推荐

