Skip to content
Open

1 #1904

Changes from 1 commit
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
37 changes: 37 additions & 0 deletions src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,54 @@
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This violates checklist item #6: 'Don't use class Objects'. The keys and values arrays should be declared with their respective generic types, K[] and V[], to improve type safety. Although you cannot instantiate a generic array directly (e.g., new K[...]), the common practice is to create an Object array and cast it, like (K[]) new Object[...].

private int size;

public StorageImpl() {
keys = new Object[MAX_SIZE];
values = new Object[MAX_SIZE];
size = 0;
}

@Override
public void put(K key, V value) {
int index = findKey(key);
if (index != -1) {
values[index] = value;
return;
}

if (size < MAX_SIZE) {
keys[size] = key;
values[size] = value;
size++;
} else {
throw new RuntimeException("Storage is full");
}
}

@Override
public V get(K key) {
int index = findKey(key);
if (index != -1) {
return (V) values[index];
}
return null;
}

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

private int findKey(K key) {
for (int i = 0; i < size; i++) {
if (keys[i] == null && key == null || (keys[i] != null && keys[i].equals(key))) {
return i;
}
}
return -1;
}
}
Loading