質問編集履歴

1

文の修正

2021/11/17 19:59

投稿

frere
frere

スコア4

test CHANGED
@@ -1 +1 @@
1
- 二分探索ジェネリクスについて
1
+ 二分探索 ジェネリクス
test CHANGED
@@ -76,322 +76,4 @@
76
76
 
77
77
 
78
78
 
79
- package exer3.pet;
80
-
81
-
82
-
83
- public class PetComparator {
84
-
85
- public static int nameOrder(Pet a, Pet b) {
86
-
87
- return a.getName().compareTo(b.getName());
88
-
89
- }
90
-
91
- }
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
- package exer3.pet;
104
-
105
- public class Pet {
106
-
107
- public String name;
108
-
109
- public Pet(){
110
-
111
- setName("");
112
-
113
- }
114
-
115
- public Pet(String name){
116
-
117
- this();
118
-
119
- setName(name);
120
-
121
- }
122
-
123
-
124
-
125
- public void setName(String name){
126
-
127
- this.name = name;
128
-
129
- }
130
-
131
-
132
-
133
- public String getName(){
134
-
135
- return name;
136
-
137
- }
138
-
139
-
140
-
141
- @Override
142
-
143
- public String toString(){
144
-
145
- return "name:" + getName();
146
-
147
- }
148
-
149
-
150
-
151
- public String pat(){
152
-
153
- String s = ":「(-.-)」";
154
-
155
- return getName() + s;
156
-
157
- }
158
-
159
-
160
-
161
- @Override
162
-
163
- public boolean equals(Object obj) {
164
-
165
- return obj.equals(name);
166
-
167
- }
168
-
169
- }
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
- package exer3.pet;
182
-
183
- public class Cat extends Pet {
184
-
185
- private boolean vac ;
186
-
187
- public Cat() {
188
-
189
- super();
190
-
191
- setVac(true);
192
-
193
- }
194
-
195
- public Cat(String name) {
196
-
197
- this();
198
-
199
- setName(name);
200
-
201
- }
202
-
203
- public void setVac(boolean vac){
204
-
205
- this.vac = vac;
206
-
207
- }
208
-
209
- public boolean isVac(){
210
-
211
- return vac;
212
-
213
- }
214
-
215
-
216
-
217
- public String pat(){
218
-
219
- String s = ":「みゃみゃみゃみゃ」";
220
-
221
- return getName() + s;
222
-
223
- }
224
-
225
- }
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
- package exer3.pet;
236
-
237
-
238
-
239
- public class Dog extends Pet {
240
-
241
- private boolean vac;
242
-
243
-
244
-
245
- public Dog() {
246
-
247
- super();
248
-
249
- setVac(true);
250
-
251
- }
252
-
253
-
254
-
255
- public Dog(String name) {
256
-
257
- this();
258
-
259
- setName(name);
260
-
261
- }
262
-
263
-
264
-
265
- public void setVac(boolean bool){
266
-
267
- vac = bool;
268
-
269
- }
270
-
271
-
272
-
273
- public boolean isVac(){
274
-
275
- return vac;
276
-
277
- }
278
-
279
-
280
-
281
- @Override
282
-
283
- public String toString() {
284
-
285
- return super.toString() +", vac.:" + (isVac() ? "yes" : "no");
286
-
287
- }
288
-
289
-
290
-
291
- public String pat(){
292
-
293
- String s = ":「わんわん」";
294
-
295
- return getName() + s;
296
-
297
- }
298
-
299
- }
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
- メインクラス
310
-
311
- package exer3.search.tester;
312
-
313
-
314
-
315
- import exer3.pet.*;
316
-
317
- import exer3.search.BinarySearch;
318
-
319
- import java.util.ArrayList;
320
-
321
- import java.io.*;
322
-
323
-
324
-
325
- public class Main {
326
-
327
- public static void main(String[] args) {
328
-
329
- ArrayList<Pet> pets = new ArrayList<>();
330
-
331
- pets.add(new Dog("Giga"));
332
-
333
- pets.add(new Pet("Hamu"));
334
-
335
- pets.add(new Dog("Jiro"));
336
-
337
- pets.add(new Pet("Kotori"));
338
-
339
- pets.add(new Dog("Shiro"));
340
-
341
- pets.add(new Cat("Tama"));
342
-
343
- pets.add(new Dog("Taro"));
344
-
345
- pets.add(new Cat("Tora"));
346
-
347
-
348
-
349
- BinarySearch<Pet> bs =
350
-
351
- new BinarySearch<>(pets,
352
-
353
- (p1, p2) -> PetComparator.nameOrder(p1, p2));
354
-
355
-
356
-
357
- BufferedReader r =
358
-
359
- new BufferedReader(new InputStreamReader(System.in));
360
-
361
-
362
-
363
- String qs;
364
-
365
-
366
-
367
- while (null != (qs = r.readLine())) {
368
-
369
- Pet q = new Pet(qs);
370
-
371
- int ans = bs.indexOf(q);
372
-
373
-
374
-
375
- if (-1 == ans)
376
-
377
- System.out.printf("%s は,配列にありません.\n", q.getName());
378
-
379
- else {
380
-
381
- System.out.printf("%s は,配列の [%d] にありました.\n",
382
-
383
-
384
-
385
- q.getName(), ans);
386
-
387
-
388
-
389
- System.out.printf("a[%d] = %s\n", ans, pets.get(ans));
390
-
391
- }
392
-
393
- }
394
-
395
- r.close();
396
-
397
79
  ```