ハッシュ法で配列変数の格納をしたいのです。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
1package exer3.hash; 2import exer3.linkedStructure.LinkedList; 3import java.util.ArrayList; 4 5public class FruitHash { 6 private class Pair { 7 String key; 8 Integer value; 9 Pair(String key, Integer value) { 10 this.key = key; 11 this.value = value; 12 } 13@Override 14public boolean equals(Object obj) { 15 return obj.equals(key); 16 } 17Integer getValue() { return value; } 18} 19 20 private ArrayList<LinkedList<Pair>> headers; 21 private int arraySize; 22 public FruitHash() { this(13); } 23 24 public FruitHash(int size) { 25 headers = new ArrayList<>(); 26 arraySize = size; 27 for (int i = 0; i < size; i += 1) 28 headers.add(new LinkedList<>()); 29 } 30 public FruitHash put(String key, Integer value) { 31 int idx = hashFunction(key) % arraySize; 32 headers.get(idx).insertA(new Pair(key, value)); 33 return this; 34 } 35 public Integer get(String key) { 36 int idx = hashFunction(key) % arraySize; 37 LinkedList<Pair> h = headers.get(idx); 38 LinkedList<Pair>.Node n = h.search(new Pair(key, null)); 39 Pair p = h.get(n); 40 return (null == p ? null : p.getValue()); 41 } 42 43 public int hashFunction(String key) { 44 return key.length(); 45 } 46 47 public void showArray(int size){ 48 49 } 50 } 51 } 52 53 54package exer3.hash.tester; 55import exer3.hash.FruitHash; 56public class TesterH04 { 57public static void main(String[] args) { 58FruitHash fh = new FruitHash(); 59fh.put("apple", 180); 60fh.put("orange", 90); 61fh.put("water melon", 900); 62fh.put("blueberry", 530); 63fh.put("raspberry", 830); 64fh.showArray(); 65 } 66}
回答1件
あなたの回答
tips
プレビュー