-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Solution #1921
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?
Solution #1921
Changes from all 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import core.basesyntax.impl.StorageImpl; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| Storage<Integer, String> storage = new StorageImpl<>(); | ||
| storage.put(1, "Value1"); | ||
| storage.put(2, "Value2"); | ||
| storage.put(3, "Value3"); | ||
|
|
||
| System.out.println("Get value by key 2: " + storage.get(2)); | ||
| System.out.println("Storage size: " + storage.size()); | ||
|
|
||
| storage.put(1, "NewValue1"); | ||
| System.out.println("Get updated value by key 1: " + storage.get(1)); | ||
|
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. Per checklist requirements, remove empty lines between method implementations. There should be no blank line after line 15 before the next method. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
|
Comment on lines
+1
to
+3
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. Missing import statement. This file cannot implement Storage interface without importing it. Add: import core.basesyntax.Storage; |
||
| private static final int MAX_SIZE = 10; | ||
| private final K[] keys; | ||
| private final V[] values; | ||
| private int size; | ||
|
|
||
| public StorageImpl() { | ||
| keys = (K[]) new Object[MAX_SIZE]; | ||
| values = (V[]) new Object[MAX_SIZE]; | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| for (int i = 0; i < size; i++) { | ||
| if (key == null && keys[i] == null || key != null && key.equals(keys[i])) { | ||
| values[i] = value; | ||
| return; | ||
| } | ||
| } | ||
| if (size < MAX_SIZE) { | ||
| keys[size] = key; | ||
| values[size] = value; | ||
| size++; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
Comment on lines
+18
to
+29
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 key-searching loop logic is duplicated in the get() method. Consider extracting this into a private helper method like findKeyIndex(K key) to follow DRY principle (checklist item #5). |
||
| public V get(K key) { | ||
| for (int i = 0; i < size; i++) { | ||
| if (key == null && keys[i] == null || key != null && key.equals(keys[i])) { | ||
| return values[i]; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
|
Comment on lines
+32
to
+39
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 key-searching loop logic duplicates code from put() method. Extract to a private helper method to follow DRY principle (checklist item #5). |
||
| public int size() { | ||
| return size; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,17 +3,44 @@ | |
| import core.basesyntax.Storage; | ||
|
|
||
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
| private static final int MAX_SIZE = 10; | ||
| private final K[] keys; | ||
| private final V[] values; | ||
| private int size; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public StorageImpl() { | ||
| keys = (K[]) new Object[MAX_SIZE]; | ||
| values = (V[]) new Object[MAX_SIZE]; | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| for (int i = 0; i < size; i++) { | ||
| if (key == keys[i] || (key != null && key.equals(keys[i]))) { | ||
|
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 will throw NullPointerException if |
||
| values[i] = value; | ||
| return; | ||
| } | ||
| } | ||
| if (size < MAX_SIZE) { | ||
| keys[size] = key; | ||
| values[size] = value; | ||
| size++; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public V get(K key) { | ||
| for (int i = 0; i < size; i++) { | ||
| if (key == keys[i] || (key != null && key.equals(keys[i]))) { | ||
|
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 has the same null-handling issue as line 20. When
Comment on lines
+19
to
+35
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 null-checking logic in |
||
| return values[i]; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return -1; | ||
| 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 constant name MAX_SIZE is not fully informative. Consider renaming to MAX_STORAGE_CAPACITY to clearly indicate its purpose.