-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Storage - first solution #1896
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?
Storage - first solution #1896
Changes from 4 commits
90da774
ecf1d38
05e423b
07b7b00
88e810c
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,17 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import core.basesyntax.impl.StorageImpl; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| StorageImpl<Integer, String> storage = new StorageImpl<>(); | ||
| String box = "box3"; | ||
| storage.put(44, box); | ||
| storage.put(22, box); | ||
| storage.put(33, box); | ||
| storage.size(); | ||
|
|
||
| System.out.println(storage.get(33)); | ||
|
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 method call has no effect because its return value is not assigned to any variable or used in any way. You can remove this line. |
||
| System.out.println(storage.size()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,17 +3,49 @@ | |
| import core.basesyntax.Storage; | ||
|
|
||
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
| private static final int MAX_STORAGE_SIZE = 10; | ||
| private K[] keys; | ||
| private V[] values; | ||
| private int size; | ||
|
|
||
| public StorageImpl() { | ||
| keys = (K[]) new Object[MAX_STORAGE_SIZE]; | ||
| values = (V[]) new Object[MAX_STORAGE_SIZE]; | ||
| size = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| if (key == null || value == null) { | ||
| throw new NullPointerException("Key or value cannot be null."); | ||
| } | ||
|
|
||
| for (int i = 0; i < size; 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. You have duplicated key‑searching logic in both Please extract this repeated logic into a private helper method, for example: private int indexOfKey(K key) { ... } Then call this method from both |
||
| if (keys[i] == key) { | ||
| values[i] = value; | ||
| return; | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+25
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 loop for finding an existing key is very similar to the one in the |
||
|
|
||
| if (size < MAX_STORAGE_SIZE) { | ||
| keys[size] = key; | ||
| values[size] = value; | ||
| size++; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public V get(K key) { | ||
| for (int i = 0; i < size; i++) { | ||
| if (keys[i] == key) { | ||
| 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.
This import is unused and can be safely removed.