回答編集履歴

1

追記

2019/10/27 08:36

投稿

rubytomato
rubytomato

スコア1752

test CHANGED
@@ -81,3 +81,97 @@
81
81
  }
82
82
 
83
83
  ```
84
+
85
+
86
+
87
+ **追記**
88
+
89
+
90
+
91
+ ```
92
+
93
+ //class listDTO {
94
+
95
+ //クラス名は大文字で始める
96
+
97
+ class ListDTO {
98
+
99
+ //public SampleItem(String id,String name) {
100
+
101
+ //コンストラクタはクラス名と同じ
102
+
103
+ public ListDTO(String id,String name) {
104
+
105
+ this.id = id;
106
+
107
+ this.name = name;
108
+
109
+ }
110
+
111
+ public String getId() {
112
+
113
+ return id;
114
+
115
+ }
116
+
117
+ public String getName() {
118
+
119
+ return name;
120
+
121
+ }
122
+
123
+ private String id;
124
+
125
+ private String name;
126
+
127
+ }
128
+
129
+ ```
130
+
131
+
132
+
133
+ Listの初期化
134
+
135
+
136
+
137
+ ```
138
+
139
+ List<ListDTO> associationList = new ArrayList<>() {
140
+
141
+ {
142
+
143
+ add(new ListDTO("1", "関東"));
144
+
145
+ add(new ListDTO("2", "関西"));
146
+
147
+ add(new ListDTO("3", "九州"));
148
+
149
+ add(new ListDTO("4", "四国"));
150
+
151
+ }
152
+
153
+ };
154
+
155
+ ```
156
+
157
+
158
+
159
+ Java9以上なら下記のように書けます
160
+
161
+
162
+
163
+ ```
164
+
165
+ List<ListDTO> lists = List.of(
166
+
167
+ new ListDTO("1", "関東"),
168
+
169
+ new ListDTO("2", "関西"),
170
+
171
+ new ListDTO("3", "九州"),
172
+
173
+ new ListDTO("4", "四国")
174
+
175
+ );
176
+
177
+ ```