-
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 1 commit
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,55 @@ | |
| import core.basesyntax.Storage; | ||
|
|
||
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
|
|
||
| public static final int MAX_SIZE = 10; | ||
| private Object[] keys = new Object[MAX_SIZE]; | ||
|
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. Violates checklist item #2: All non-constant fields should be initialized in the constructor, not inline. |
||
| private Object[] values = new Object[MAX_SIZE]; | ||
|
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. Violates checklist item #2: All non-constant fields should be initialized in the constructor, not inline. |
||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| for (int i = 0; i < keys.length; i++) { | ||
| if (keys[i] != null && keys[i].equals(key)) { | ||
| values[i] = value; | ||
| return; | ||
| } | ||
| 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. Violates checklist item #5 (DRY): This condition duplicates the logic from lines 14 and 18. The reference check |
||
| values[i] = value; | ||
| return; | ||
| } | ||
| if (keys[i] == null && 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. CRITICAL BUG: This condition is wrong. It 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) { | ||
|
|
||
|
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. Violates checklist item #1: Remove empty line before method implementation. |
||
| for (int i = 0; i < keys.length; i++) { | ||
| if (keys[i] != null && keys[i].equals(key)) { | ||
| return (V) values[i]; | ||
| } | ||
| if (keys[i] == null && key == 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. Violates checklist item #5 (DRY): Duplicate logic from line 14. The condition |
||
| 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; | ||
|
|
||
|
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. Violates checklist item #1: Remove empty line before method implementation. |
||
| 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.
Violates checklist item #1: Remove all redundant empty lines. Do not begin class or method implementation with empty line.