回答編集履歴

4

TreeMap の使用の提案

2020/04/10 02:40

投稿

jimbe
jimbe

スコア12754

test CHANGED
@@ -31,3 +31,143 @@
31
31
 
32
32
 
33
33
  なお, この状態は @SuppressWarnings など付けずに ```List<dictionarySet> dictionaryList``` ときちんと型を書いてあれば, Eclipse において該当行で「ありそうもない Collenction<dictionarySet> の remove(Object) の引数型 String」(Plaiades訳) という警告が出て気付けます.
34
+
35
+
36
+
37
+ #追記
38
+
39
+ put が「word が重複したら上書き」するというのであれば, そして内部では word でソートするのであれば, [TreeMap](https://docs.oracle.com/javase/jp/8/docs/api/java/util/TreeMap.html) を使用しては如何でしょうか.
40
+
41
+ 自分で探して削除したりソートしたりは必要無くなります.
42
+
43
+ ```java
44
+
45
+ package teratail.q252580;
46
+
47
+ //package dictionary;
48
+
49
+
50
+
51
+ import java.util.*;
52
+
53
+
54
+
55
+ public class Dictionary {
56
+
57
+ private Map<String,Item> map = new TreeMap<>();
58
+
59
+ private int displayNumber;
60
+
61
+
62
+
63
+ public static void main(String args[]) {
64
+
65
+ Dictionary dic = new Dictionary();
66
+
67
+ dic.put("B", "BBB");
68
+
69
+ dic.put("C", "CCC");
70
+
71
+ dic.put("A", "AAA");
72
+
73
+ dic.printAll();
74
+
75
+ System.out.println();
76
+
77
+ dic.put("B", "BBBbbbbb");
78
+
79
+ dic.printAll();
80
+
81
+ }
82
+
83
+ public int getDisplayNumber() {
84
+
85
+ return displayNumber;
86
+
87
+ }
88
+
89
+ public void setDisplayNumber(int displayNumber) {
90
+
91
+ this.displayNumber = displayNumber;
92
+
93
+ }
94
+
95
+ public void put(String word, String explanation) {
96
+
97
+ map.put(word, new Item(word, explanation));
98
+
99
+ }
100
+
101
+ public void printAll() {
102
+
103
+ map.forEach((k,v) -> System.out.println(v));
104
+
105
+ }
106
+
107
+ public void getOnePage(int page) {
108
+
109
+ }
110
+
111
+ public void clear() {
112
+
113
+ map.clear();
114
+
115
+ }
116
+
117
+
118
+
119
+ private static class Item implements Comparable<Item>{
120
+
121
+ private String word;
122
+
123
+ private String explanation;
124
+
125
+
126
+
127
+ public Item(String word, String explanation) {
128
+
129
+ this.word = word;
130
+
131
+ this.explanation = explanation;
132
+
133
+ }
134
+
135
+ @Override
136
+
137
+ public int compareTo(Item item){
138
+
139
+ return word.compareTo(item.word);
140
+
141
+ }
142
+
143
+ @Override
144
+
145
+ public String toString() {
146
+
147
+ return word + ":" + explanation;
148
+
149
+ }
150
+
151
+ }
152
+
153
+ }
154
+
155
+ ```
156
+
157
+ ```plain text
158
+
159
+ A:AAA
160
+
161
+ B:BBB
162
+
163
+ C:CCC
164
+
165
+
166
+
167
+ A:AAA
168
+
169
+ B:BBBbbbbb
170
+
171
+ C:CCC
172
+
173
+ ```

3

追加

2020/04/10 02:40

投稿

jimbe
jimbe

スコア12754

test CHANGED
@@ -24,9 +24,9 @@
24
24
 
25
25
 
26
26
 
27
- ですので, word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.
27
+ ですので, (word が null で無ければ) word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.
28
28
 
29
- word は String であり, dictionaryList.get(i) は dictionarySet ですから, どうやっても削除されないでしょう.
29
+ word は String であり, dictionaryList.get(i) は dictionarySet ですから, どうやっても削除されないでしょう. ( dictionarySet には equals や hashCode の定義もありません.)
30
30
 
31
31
 
32
32
 

2

追加修正

2020/04/09 18:26

投稿

jimbe
jimbe

スコア12754

test CHANGED
@@ -25,3 +25,9 @@
25
25
 
26
26
 
27
27
  ですので, word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.
28
+
29
+ word は String であり, dictionaryList.get(i) は dictionarySet ですから, どうやっても削除されないでしょう.
30
+
31
+
32
+
33
+ なお, この状態は @SuppressWarnings など付けずに ```List<dictionarySet> dictionaryList``` ときちんと型を書いてあれば, Eclipse において該当行で「ありそうもない Collenction<dictionarySet> の remove(Object) の引数型 String」(Plaiades訳) という警告が出て気付けます.

1

全面変更

2020/04/09 17:58

投稿

jimbe
jimbe

スコア12754

test CHANGED
@@ -1,3 +1,27 @@
1
- iterator を使用中にその元である List から直接 remove することはできません.
1
+ ~~iterator を使用中にその元である List から直接 remove することはできません.
2
2
 
3
- iterator の remove メソッドを使うか, iterator を使わずにループしてください.
3
+ iterator の remove メソッドを使うか, iterator を使わずにループしてください.~~
4
+
5
+
6
+
7
+ 失礼しました.
8
+
9
+ false というのは
10
+
11
+ ```
12
+
13
+ System.out.println(dictionaryList.remove(word));
14
+
15
+ ```
16
+
17
+ のことですね
18
+
19
+
20
+
21
+ [remove](https://docs.oracle.com/javase/jp/8/docs/api/java/util/List.html#remove-java.lang.Object-)
22
+
23
+ > (o==null ? get(i)==null : o.equals(get(i)))となる、最小のインデックス値iを持つ要素を削除します
24
+
25
+
26
+
27
+ ですので, word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.