Reset size to 0
when clearing table
This commit is contained in:
parent
13991b6bd2
commit
1d0e670597
|
@ -1,129 +1,130 @@
|
||||||
package ch.nuth.zhaw.exbox;
|
package ch.nuth.zhaw.exbox;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public class MyHashtable<K, V> implements Map<K, V> {
|
public class MyHashtable<K, V> implements Map<K, V> {
|
||||||
private int capacity;
|
private int capacity;
|
||||||
private int size;
|
private int size;
|
||||||
private K[] keys = (K[]) new Object[10];
|
private K[] keys = (K[]) new Object[10];
|
||||||
private V[] values = (V[]) new Object[10];
|
private V[] values = (V[]) new Object[10];
|
||||||
|
|
||||||
public MyHashtable(int size) {
|
public MyHashtable(int size) {
|
||||||
capacity = size;
|
capacity = size;
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hashEquals(Object x, Object y) {
|
private boolean hashEquals(Object x, Object y) {
|
||||||
if (x == null || y == null) {
|
if (x == null || y == null) {
|
||||||
return x == y;
|
return x == y;
|
||||||
} else {
|
} else {
|
||||||
return x.hashCode() == y.hashCode();
|
return x.hashCode() == y.hashCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int hash(Object k) {
|
private int hash(Object k) {
|
||||||
int h = Math.abs(k.hashCode());
|
int h = Math.abs(k.hashCode());
|
||||||
return h % keys.length;
|
return h % keys.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes all mappings from this map (optional operation).
|
// Removes all mappings from this map (optional operation).
|
||||||
public void clear() {
|
public void clear() {
|
||||||
keys = (K[]) new Object[capacity];
|
size = 0;
|
||||||
values = (V[]) new Object[capacity];
|
keys = (K[]) new Object[capacity];
|
||||||
}
|
values = (V[]) new Object[capacity];
|
||||||
|
}
|
||||||
// Associates the specified value with the specified key in this map (optional
|
|
||||||
// operation).
|
// Associates the specified value with the specified key in this map (optional
|
||||||
public V put(K key, V value) {
|
// operation).
|
||||||
int hashCode = hash(key);
|
public V put(K key, V value) {
|
||||||
int index = hashCode;
|
int hashCode = hash(key);
|
||||||
|
int index = hashCode;
|
||||||
for (int i = 0; keys[index] != null && !hashEquals(keys[index], key); i++) {
|
|
||||||
index = hash(hashCode + (int) Math.pow(i, 2));
|
for (int i = 0; keys[index] != null && !hashEquals(keys[index], key); i++) {
|
||||||
}
|
index = hash(hashCode + (int) Math.pow(i, 2));
|
||||||
|
}
|
||||||
if (keys[index] == null) {
|
|
||||||
size++;
|
if (keys[index] == null) {
|
||||||
}
|
size++;
|
||||||
|
}
|
||||||
keys[index] = key;
|
|
||||||
values[index] = value;
|
keys[index] = key;
|
||||||
|
values[index] = value;
|
||||||
return value;
|
|
||||||
}
|
return value;
|
||||||
|
}
|
||||||
// Returns the value to which this map maps the specified key.
|
|
||||||
public V get(Object key) {
|
// Returns the value to which this map maps the specified key.
|
||||||
// to be done
|
public V get(Object key) {
|
||||||
for (int i = 0; i < keys.length; i++) {
|
// to be done
|
||||||
if (hashEquals(key, keys[i])) {
|
for (int i = 0; i < keys.length; i++) {
|
||||||
return values[i];
|
if (hashEquals(key, keys[i])) {
|
||||||
}
|
return values[i];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
// Removes the mapping for this key from this map if present (optional
|
|
||||||
// operation).
|
// Removes the mapping for this key from this map if present (optional
|
||||||
public V remove(Object key) {
|
// operation).
|
||||||
// to be done (Aufgabe 3)
|
public V remove(Object key) {
|
||||||
throw new UnsupportedOperationException();
|
// to be done (Aufgabe 3)
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Returns the number of key-value mappings in this map.
|
|
||||||
public int size() {
|
// Returns the number of key-value mappings in this map.
|
||||||
return size;
|
public int size() {
|
||||||
}
|
return size;
|
||||||
|
}
|
||||||
// UnsupportedOperationException
|
|
||||||
// ===================================================================
|
// UnsupportedOperationException
|
||||||
// Returns a collection view of the values contained in this map.
|
// ===================================================================
|
||||||
public Collection<V> values() {
|
// Returns a collection view of the values contained in this map.
|
||||||
throw new UnsupportedOperationException();
|
public Collection<V> values() {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Returns true if this map contains a mapping for the specified key.
|
|
||||||
public boolean containsKey(Object key) {
|
// Returns true if this map contains a mapping for the specified key.
|
||||||
throw new UnsupportedOperationException();
|
public boolean containsKey(Object key) {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Returns true if this map maps one or more keys to the specified value.
|
|
||||||
public boolean containsValue(Object value) {
|
// Returns true if this map maps one or more keys to the specified value.
|
||||||
throw new UnsupportedOperationException();
|
public boolean containsValue(Object value) {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Returns a set view of the mappings contained in this map.
|
|
||||||
public Set<Map.Entry<K, V>> entrySet() {
|
// Returns a set view of the mappings contained in this map.
|
||||||
throw new UnsupportedOperationException();
|
public Set<Map.Entry<K, V>> entrySet() {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Compares the specified object with this map for equality.
|
|
||||||
public boolean equals(Object o) {
|
// Compares the specified object with this map for equality.
|
||||||
throw new UnsupportedOperationException();
|
public boolean equals(Object o) {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Copies all of the mappings from the specified map to this map (optional
|
|
||||||
// operation).
|
// Copies all of the mappings from the specified map to this map (optional
|
||||||
public void putAll(Map<? extends K, ? extends V> t) {
|
// operation).
|
||||||
throw new UnsupportedOperationException();
|
public void putAll(Map<? extends K, ? extends V> t) {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Returns the hash code value for this map.
|
|
||||||
public int hashCode() {
|
// Returns the hash code value for this map.
|
||||||
throw new UnsupportedOperationException();
|
public int hashCode() {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Returns true if this map contains no key-value mappings.
|
|
||||||
public boolean isEmpty() {
|
// Returns true if this map contains no key-value mappings.
|
||||||
throw new UnsupportedOperationException();
|
public boolean isEmpty() {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
// Returns a set view of the keys contained in this map.
|
|
||||||
public Set<K> keySet() {
|
// Returns a set view of the keys contained in this map.
|
||||||
throw new UnsupportedOperationException();
|
public Set<K> keySet() {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue