-
Notifications
You must be signed in to change notification settings - Fork 1.7k
First solution by Viodon #1914
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
base: master
Are you sure you want to change the base?
First solution by Viodon #1914
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,17 +3,54 @@ | |
| import core.basesyntax.Storage; | ||
|
|
||
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
| public static final int MAX_SIZE = 10; | ||
| private Object[] keys; | ||
| private Object[] values; | ||
|
|
||
| public StorageImpl() { | ||
| keys = new Object[MAX_SIZE]; | ||
| values = new Object[MAX_SIZE]; | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| for (int i = 0; i < keys.length; i++) { | ||
| if (keys[i] != null && keys[i].equals(key) || keys[i] == key) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant check: The |
||
| values[i] = value; | ||
| return; | ||
| } | ||
|
|
||
| if (values[i] == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The empty slot detection checks
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Шукає вільний слот для значення коли ключ не збігається з жодним іншим, перевіряємо values[і], перевіряємо values[i], тому що може бути значення з ключем null, @mateacademy-ai-mentor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL BUG: Empty slot detection checks |
||
| values[i] = value; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL BUG: After fixing the empty slot condition, this line should set |
||
| keys[i] = key; | ||
| return; | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public V get(K key) { | ||
| for (int i = 0; i < keys.length; i++) { | ||
| if (keys[i] != null && keys[i].equals(key)) { | ||
| return (V) values[i]; | ||
| } | ||
| if (keys[i] == key) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant check: Same issue as in |
||
| return (V) values[i]; | ||
|
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mateacademy-ai-mentor ця стрічка потрібна, вона перевіряє ключ null, в рядку 34 значення порівнюються лише якщо keys[i] != null, щоб уникнути помилки NullPointerExceptions
Comment on lines
+36
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DRY violation: The return |
||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return -1; | ||
| int size = 0; | ||
| for (int i = 0; i < values.length; i++) { | ||
| if (values[i] != null) { | ||
| size++; | ||
| } | ||
| } | ||
| return size; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reference equality check
keys[i] == keyis redundant. The conditionkeys[i].equals(key)already handles all cases including when both are null, so this OR clause can be removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mateacademy-ai-mentor строка потрібна, ввона не обробляє варіанти коли ключ = null, без неї код працює не коректно