質問編集履歴

1

エラーの追加

2018/05/31 05:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
 
12
12
 
13
+ >
14
+
15
+
16
+
13
17
 
14
18
 
15
19
  ```java
@@ -32,6 +36,8 @@
32
36
 
33
37
  */
34
38
 
39
+
40
+
35
41
  try {
36
42
 
37
43
  FileReader in = new FileReader(file1);
@@ -103,3 +109,317 @@
103
109
  }
104
110
 
105
111
  ```
112
+
113
+
114
+
115
+ ```java
116
+
117
+ public class Lesson{
118
+
119
+ static DoublyListInt head; // ポインタ(前方)
120
+
121
+ static DoublyListInt tail; // ポインタ(後方)
122
+
123
+ static DoublyListInt ptr; // 作業用ポインタ
124
+
125
+ static int n; // データ
126
+
127
+ static int i; // ループカウンタ
128
+
129
+ static int k; // 作業用カウンタ
130
+
131
+ static int count; // 比較回数
132
+
133
+ static Random rnd = new Random();
134
+
135
+ public static void main(String[] args) {
136
+
137
+ head = null;
138
+
139
+ tail = null;
140
+
141
+ ptr = null;
142
+
143
+ n = 10;
144
+
145
+ count = 0;
146
+
147
+
148
+
149
+
150
+
151
+ DoublyListInt list = new DoublyListInt(count, head);
152
+
153
+ String file1 = "ファイル名";
154
+
155
+ String file2 = "ファイル名";
156
+
157
+
158
+
159
+ //file1からデータを入力
160
+
161
+ try {
162
+
163
+ FileReader fr = new FileReader(file1);
164
+
165
+ BufferedReader br = new BufferedReader(fr);
166
+
167
+ String line;
168
+
169
+ while ((line = br.readLine()) != null) {
170
+
171
+ for (int i = 0; i < 10; i++) {
172
+
173
+
174
+
175
+ push(line);
176
+
177
+ }
178
+
179
+
180
+
181
+ }
182
+
183
+ br.close();
184
+
185
+ }
186
+
187
+ catch (FileNotFoundException e) {
188
+
189
+ System.out.println(file1+"ファイルが見つかりません。");
190
+
191
+ }
192
+
193
+ catch (IOException e) {
194
+
195
+ System.out.println(e);
196
+
197
+ }
198
+
199
+
200
+
201
+ bubbleSort();
202
+
203
+ System.out.println(list);
204
+
205
+
206
+
207
+ //file2に結果を出力
208
+
209
+ try {
210
+
211
+ BufferedReader br = new BufferedReader(new FileReader(file1));
212
+
213
+ PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter(file2)));
214
+
215
+ br.readLine();
216
+
217
+ pr.println(list);
218
+
219
+ br.close();
220
+
221
+ pr.close();
222
+
223
+ } catch (IOException e) {
224
+
225
+ System.out.println(e);
226
+
227
+ }
228
+
229
+ }
230
+
231
+
232
+
233
+ static int rand(int x) {
234
+
235
+ int n;
236
+
237
+ n = rnd.nextInt(x);
238
+
239
+ return n;
240
+
241
+ }
242
+
243
+
244
+
245
+ private static void push(int p) {
246
+
247
+ head = new DoublyListInt(p, head);
248
+
249
+ if (tail == null) {
250
+
251
+ tail = head;
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+ static void bubbleSort() {
260
+
261
+ for (int k = 0; k < n; k++) {
262
+
263
+ ptr = head;
264
+
265
+ for (int j = n; k < j; j--) {
266
+
267
+ boolean ex = compare();
268
+
269
+ if (ex) {
270
+
271
+ swap(ptr);
272
+
273
+ }
274
+
275
+ count++;
276
+
277
+ if (ptr != null && ptr.next != null) {
278
+
279
+ ptr = ptr.next;
280
+
281
+ }
282
+
283
+ }
284
+
285
+ }
286
+
287
+ }
288
+
289
+
290
+
291
+ static boolean compare() {
292
+
293
+ if (ptr != null && ptr.next != null) {
294
+
295
+ int s = ptr.value;
296
+
297
+ int t = ptr.next.value;
298
+
299
+ if (s < t) {
300
+
301
+ System.out.println(s + "<" + t);
302
+
303
+ return true;
304
+
305
+ } else {
306
+
307
+ System.out.println(s + ">" + t);
308
+
309
+ }
310
+
311
+
312
+
313
+ }
314
+
315
+ return false;
316
+
317
+ }
318
+
319
+
320
+
321
+ static void swap(DoublyListInt y) {
322
+
323
+ DoublyListInt a = null;
324
+
325
+ DoublyListInt b = null;
326
+
327
+
328
+
329
+ if ((y != null) && (y.next != null)) {
330
+
331
+ a = y;
332
+
333
+ b = a.next;
334
+
335
+
336
+
337
+ if (a.prev == null) {
338
+
339
+ head = b;
340
+
341
+ } else {
342
+
343
+ a.prev.next = b;
344
+
345
+ }
346
+
347
+
348
+
349
+ if (b.next == null) {
350
+
351
+ tail = a;
352
+
353
+ } else {
354
+
355
+ b.next.prev = a;
356
+
357
+ }
358
+
359
+
360
+
361
+ b.prev = a.prev;
362
+
363
+ a.prev = b;
364
+
365
+
366
+
367
+ a.next = b.next;
368
+
369
+ b.next = a;
370
+
371
+
372
+
373
+ System.out.println();
374
+
375
+ System.out.println("結果");
376
+
377
+ showdata();
378
+
379
+ ptr = y.prev;
380
+
381
+ }
382
+
383
+ }
384
+
385
+ static void showdata() {
386
+
387
+ ptr = tail;
388
+
389
+ for (int i = 0; i < n; i++) {
390
+
391
+ if (ptr != null) {
392
+
393
+ int v = ptr.value;
394
+
395
+ System.out.println(i + "\t" + v);
396
+
397
+ ptr = ptr.prev;
398
+
399
+ }
400
+
401
+ }
402
+
403
+ System.out.println();
404
+
405
+ }
406
+
407
+
408
+
409
+ }
410
+
411
+ ```
412
+
413
+ 自分なりにやってみた結果
414
+
415
+
416
+
417
+ DoublyListInt を型に解決できません
418
+
419
+ 型 Lesson のメソッド push(int) は引数 (String) に適用できません
420
+
421
+
422
+
423
+ とでできました。
424
+
425
+ どこをどのように直したらいいでしょうか。