teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

TreeMap の使用の提案

2020/04/10 02:40

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -14,4 +14,74 @@
14
14
  ですので, (word が null で無ければ) word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.
15
15
  word は String であり, dictionaryList.get(i) は dictionarySet ですから, どうやっても削除されないでしょう. ( dictionarySet には equals や hashCode の定義もありません.)
16
16
 
17
- なお, この状態は @SuppressWarnings など付けずに ```List<dictionarySet> dictionaryList``` ときちんと型を書いてあれば, Eclipse において該当行で「ありそうもない Collenction<dictionarySet> の remove(Object) の引数型 String」(Plaiades訳) という警告が出て気付けます.
17
+ なお, この状態は @SuppressWarnings など付けずに ```List<dictionarySet> dictionaryList``` ときちんと型を書いてあれば, Eclipse において該当行で「ありそうもない Collenction<dictionarySet> の remove(Object) の引数型 String」(Plaiades訳) という警告が出て気付けます.
18
+
19
+ #追記
20
+ put が「word が重複したら上書き」するというのであれば, そして内部では word でソートするのであれば, [TreeMap](https://docs.oracle.com/javase/jp/8/docs/api/java/util/TreeMap.html) を使用しては如何でしょうか.
21
+ 自分で探して削除したりソートしたりは必要無くなります.
22
+ ```java
23
+ package teratail.q252580;
24
+ //package dictionary;
25
+
26
+ import java.util.*;
27
+
28
+ public class Dictionary {
29
+ private Map<String,Item> map = new TreeMap<>();
30
+ private int displayNumber;
31
+
32
+ public static void main(String args[]) {
33
+ Dictionary dic = new Dictionary();
34
+ dic.put("B", "BBB");
35
+ dic.put("C", "CCC");
36
+ dic.put("A", "AAA");
37
+ dic.printAll();
38
+ System.out.println();
39
+ dic.put("B", "BBBbbbbb");
40
+ dic.printAll();
41
+ }
42
+ public int getDisplayNumber() {
43
+ return displayNumber;
44
+ }
45
+ public void setDisplayNumber(int displayNumber) {
46
+ this.displayNumber = displayNumber;
47
+ }
48
+ public void put(String word, String explanation) {
49
+ map.put(word, new Item(word, explanation));
50
+ }
51
+ public void printAll() {
52
+ map.forEach((k,v) -> System.out.println(v));
53
+ }
54
+ public void getOnePage(int page) {
55
+ }
56
+ public void clear() {
57
+ map.clear();
58
+ }
59
+
60
+ private static class Item implements Comparable<Item>{
61
+ private String word;
62
+ private String explanation;
63
+
64
+ public Item(String word, String explanation) {
65
+ this.word = word;
66
+ this.explanation = explanation;
67
+ }
68
+ @Override
69
+ public int compareTo(Item item){
70
+ return word.compareTo(item.word);
71
+ }
72
+ @Override
73
+ public String toString() {
74
+ return word + ":" + explanation;
75
+ }
76
+ }
77
+ }
78
+ ```
79
+ ```plain text
80
+ A:AAA
81
+ B:BBB
82
+ C:CCC
83
+
84
+ A:AAA
85
+ B:BBBbbbbb
86
+ C:CCC
87
+ ```

3

追加

2020/04/10 02:40

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -11,7 +11,7 @@
11
11
  [remove](https://docs.oracle.com/javase/jp/8/docs/api/java/util/List.html#remove-java.lang.Object-)
12
12
  > (o==null ? get(i)==null : o.equals(get(i)))となる、最小のインデックス値iを持つ要素を削除します
13
13
 
14
- ですので, word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.
14
+ ですので, (word が null で無ければ) word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.
15
- word は String であり, dictionaryList.get(i) は dictionarySet ですから, どうやっても削除されないでしょう.
15
+ word は String であり, dictionaryList.get(i) は dictionarySet ですから, どうやっても削除されないでしょう. ( dictionarySet には equals や hashCode の定義もありません.)
16
16
 
17
17
  なお, この状態は @SuppressWarnings など付けずに ```List<dictionarySet> dictionaryList``` ときちんと型を書いてあれば, Eclipse において該当行で「ありそうもない Collenction<dictionarySet> の remove(Object) の引数型 String」(Plaiades訳) という警告が出て気付けます.

2

追加修正

2020/04/09 18:26

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -11,4 +11,7 @@
11
11
  [remove](https://docs.oracle.com/javase/jp/8/docs/api/java/util/List.html#remove-java.lang.Object-)
12
12
  > (o==null ? get(i)==null : o.equals(get(i)))となる、最小のインデックス値iを持つ要素を削除します
13
13
 
14
- ですので, word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.
14
+ ですので, word.equals(dictionaryList.get(i)) が成立しなければ削除にはなりません.
15
+ word は String であり, dictionaryList.get(i) は dictionarySet ですから, どうやっても削除されないでしょう.
16
+
17
+ なお, この状態は @SuppressWarnings など付けずに ```List<dictionarySet> dictionaryList``` ときちんと型を書いてあれば, Eclipse において該当行で「ありそうもない Collenction<dictionarySet> の remove(Object) の引数型 String」(Plaiades訳) という警告が出て気付けます.

1

全面変更

2020/04/09 17:58

投稿

jimbe
jimbe

スコア13355

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