[JAVA] A memo that handles a class created independently with ArrayList

Handle objects created independently with ArrayList

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

A memo that handles a class created independently with ArrayList
[Rails] A memo that created an advanced search form with ransack
How to test a class that handles application.properties with SpringBoot (request: pointed out)
A memo that touched Spring Boot
[Ruby] Processing with multiple colons. A class that utilizes the module namespace.
A memo that installed Ubuntu 20.4 on HP Z2 Mini G4 and created a deep learning environment with Python Docker
ArrayList class
Point memo that was moss when building a docker container created by a colleague
[Memo] Create a CentOS 8 environment easily with Docker
How to create a class that inherits class information
A memo for myself that object-oriented is something
A memo that I was addicted to when making batch processing with Spring Boot
A story that stumbled when deploying a web application created with Spring Boot to EC2