Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,47 @@
import core.basesyntax.Storage;

public class StorageImpl<K, V> implements Storage<K, V> {
private static final int MAX_SIZE = 10;
private Object[] keys;
private Object[] values;
private int size;

public StorageImpl() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All non-constant fields must be initialized in the constructor per the checklist: "All non-constant fields should be initialized in the constructor". size is declared but not explicitly initialized in the constructor — add size = 0; (or set it explicitly) inside the constructor.

keys = new Object[MAX_SIZE];
values = new Object[MAX_SIZE];
size = 0;
}

@Override
public void put(K key, V value) {
for (int i = 0; i < size; i++) {
if (keysEqual(keys[i], key)) {
values[i] = value;
return;
}
}
keys[size] = key;
values[size] = value;
size++;
}

@Override
public V get(K key) {
for (int i = 0; i < size; i++) {
if (keysEqual(key, keys[i])) {
return (V) values[i];
}
}
return null;
}

@Override
public int size() {
return -1;
return size;
}

private boolean keysEqual(Object keyInStorage, Object keyToFind) {
return keyInStorage == keyToFind
|| (keyInStorage != null && keyInStorage.equals(keyToFind));
}
}
Loading