Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first solution #1682

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
31 changes: 30 additions & 1 deletion src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
package core.basesyntax.impl;

import core.basesyntax.Storage;
import java.util.Objects;

public class StorageImpl<K, V> implements Storage<K, V> {
private Object[] keys;
private Object[] values;
private int index;
Copy link

Choose a reason for hiding this comment

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

Rename this variable, so the name will better suit it's purpose.

Suggested change
private int index;
private int size;


public StorageImpl() {
int maxArrSize = 10;
Copy link

Choose a reason for hiding this comment

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

Replace this local variable with constant field: private static final int MAX_SIZE = 10;.

this.keys = new Object[maxArrSize];
this.values = new Object[maxArrSize];
this.index = 0;
}

@Override
public void put(K key, V value) {

for (int i = 0; i < index; i++) {
if (Objects.equals(keys[i], key)) {
values[i] = value;
return;
}
}

keys[index] = key;
values[index] = value;
index++;
Comment on lines +28 to +30
Copy link

Choose a reason for hiding this comment

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

Add check to ensure that maximum capacity is not exceeded.

Suggested change
keys[index] = key;
values[index] = value;
index++;
if (size < MAX_SIZE) {
keys[size] = key;
values[size] = value;
size++;
} else {
throw new IllegalStateException("Storage is full. Maximum capacity is " + MAX_SIZE);
}


}

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

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