I want to get the field name of the [Java] field. (Old tale tone)

Once upon a time, there were the following fields in one place.

public class Xxx {
	public String xxx1;
	public String xxx2;
	public String xxx3;
	public String xxx4;
	public String xxx5;
	public String xxx6;
	public String xxx7;
	public String xxx8;
	public String xxx9;
	public String xxx10;

And there was the following List.

List<String> list = new ArrayList<String>();

Someone said. "I want you to loop through the list and set the fields in order from 1."

Developer A has implemented the following.

Xxx x = new Xxx();
int idx = 0;
x.xxx1  = (idx < list.size()) ? list.get(idx) : null;
x.xxx2  = (idx < list.size()) ? list.get(idx) : null;
x.xxx3  = (idx < list.size()) ? list.get(idx) : null;
x.xxx4  = (idx < list.size()) ? list.get(idx) : null;
x.xxx5  = (idx < list.size()) ? list.get(idx) : null;
x.xxx6  = (idx < list.size()) ? list.get(idx) : null;
x.xxx7  = (idx < list.size()) ? list.get(idx) : null;
x.xxx8  = (idx < list.size()) ? list.get(idx) : null;
x.xxx9  = (idx < list.size()) ? list.get(idx) : null;
x.xxx10 = (idx < list.size()) ? list.get(idx) : null;

Developer B has implemented the following.

Xxx x = new Xxx();
for (int idx = 0; idx < list.size(); idx++) {
	x.getClass().getField("xxx" + (idx + 1)).set(x, list.get(idx));

that's all.

Recommended Posts

I want to get the field name of the [Java] field. (Old tale tone)
I want to display the name of the poster of the comment
I want to output the day of the week
I want to var_dump the contents of the intent
I want to get the IP address when connecting to Wi-Fi in Java
[Java] How to get the authority of the folder
I want you to use Enum # name () for the Key of SharedPreference
I want to get the value in Ruby
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to recursively get the superclass and interface of a certain class
[Java] I want to calculate the difference from the date
[Java] How to get the maximum value of HashMap
I want to know the answer of the rock-paper-scissors app
I want to get along with Map [Java beginner]
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to get a list of only unique character strings by excluding fixed character strings from the file name
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
# 1_JAVA I want to get the index number by specifying one character in the character string.
How to get the class name / method name running in Java
I tried to summarize the basics of kotlin and java
Get to the abbreviations from 5 examples of iterating Java lists
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
I want to give a class name to the select attribute
I want to narrow down the display of docker ps
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to simplify the conditional if-else statement in Java
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
I want to find out which version of java the jar file I have is available
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I want to get a list of the contents of a zip file and its uncompressed size
I want to return to the previous screen with kotlin and java!
The story of Collectors.groupingBy that I want to keep for posterity
I want to limit the input by narrowing the range of numbers
I tried to summarize the methods of Java String and StringBuilder
[Java] I want to perform distinct with the key in the object
I want to control the default error message of Spring Boot
I want to change the value of Attribute in Selenium of Ruby
[Android] I want to get the listener from the button in ListView
How to get the length of an audio file in java
I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
Get the result of POST in Java
[Java] Get the day of the specific day of the week
[Java] How to get the current directory
[day: 5] I summarized the basics of Java
How to get the date in java
I want to stop Java updates altogether
Output of the book "Introduction to Java"
I went to the Java Women's Club # 1
I want to display the number of orders for today using datetime.
I want to display images with REST Controller of Java and Spring!
I want to know the JSP of the open portlet when developing Liferay
[Ruby] I want to extract only the value of the hash and only the key
I want to pass the argument of Annotation and the argument of the calling method to aspect
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
I translated the grammar of R and Java [Updated from time to time]
graphql-ruby: How to get the name of query or mutation in controller Note
[RxSwift] I want to deepen my understanding by following the definition of Observable