ハッシュ法で配列変数の格納をしたいのです。showArrayメソッドを追加して以下の実行を得たいのですがどうすればよいでしょうか?showArrayメソッドの作り方がわかりません。
[0] : 0
[1] : 0
[2] : 0
[3] : 0
[4] : 0
[5] : 1 (apple,180)
[6] : 1 (orange,90)
[7] : 0
[8] : 0
[9] : 2 (raspberry,830)->(blueberry,530)
[10] : 0
[11] : 1 (water melon,900)
[12] : 0
Java
package exer3.hash; import exer3.linkedStructure.LinkedList; import java.util.ArrayList; public class FruitHash { private class Pair { String key; Integer value; Pair(String key, Integer value) { this.key = key; this.value = value; } @Override public boolean equals(Object obj) { return obj.equals(key); } Integer getValue() { return value; } } private ArrayList<LinkedList<Pair>> headers; private int arraySize; public FruitHash() { this(13); } public FruitHash(int size) { headers = new ArrayList<>(); arraySize = size; for (int i = 0; i < size; i += 1) headers.add(new LinkedList<>()); } public FruitHash put(String key, Integer value) { int idx = hashFunction(key) % arraySize; headers.get(idx).insertA(new Pair(key, value)); return this; } public Integer get(String key) { int idx = hashFunction(key) % arraySize; LinkedList<Pair> h = headers.get(idx); LinkedList<Pair>.Node n = h.search(new Pair(key, null)); Pair p = h.get(n); return (null == p ? null : p.getValue()); } public int hashFunction(String key) { return key.length(); } public void showArray(int size){ } } } package exer3.hash.tester; import exer3.hash.FruitHash; public class TesterH04 { public static void main(String[] args) { FruitHash fh = new FruitHash(); fh.put("apple", 180); fh.put("orange", 90); fh.put("water melon", 900); fh.put("blueberry", 530); fh.put("raspberry", 830); fh.showArray(); } }
まだ回答がついていません
会員登録して回答してみよう