Nice to meet you. It's a horse. I will write a note when dealing with a list of my own classes. ** Example: Handle a list of your own class "Food" ** Instantiate Food in the argument of the add method and add it to the list.
List<Food> foodList = new ArrayList<>();
foodList.add(new Food("banana","fruit"));
foodList.add(new Food("cabbage","Vegetables"));
foodList.add(new Food("curry","cuisine"));
To access the element ... (1) Prepare a variable of Food class, get it from the list and assign it to it. (2) Use the variable to process your own class (3) Use of extended for statement
Food fd = foodList.get(0); // (1)
fd.printFood(); // (2)
fd.setFoodName("Apple"); // (2)
for(Food f : foodList) { // (3)
f.printFood();
}
Recommended Posts