-
Notifications
You must be signed in to change notification settings - Fork 1.7k
implemented a few methods #1905
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?
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 |
|---|---|---|
| @@ -1,19 +1,44 @@ | ||
| package core.basesyntax.impl; | ||
|
|
||
| import core.basesyntax.Storage; | ||
| import java.util.Objects; | ||
|
|
||
| public class StorageImpl<K, V> implements Storage<K, V> { | ||
| private static final int MAX_SIZE = 10; | ||
| private Object[] keys; | ||
| private Object[] values; | ||
| private int size; | ||
|
|
||
| public StorageImpl() { | ||
|
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. All non-constant fields must be initialized in the constructor per the checklist: "All non-constant fields should be initialized in the constructor". |
||
| keys = new Object[MAX_SIZE]; | ||
| values = new Object[MAX_SIZE]; | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| for (int i = 0; i < size; i++) { | ||
| if (Objects.equals(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. The equality check here duplicates the same logic used in get(). This violates the checklist item: "Don't create repeating code". Extract a private null-safe comparison method (for example |
||
| values[i] = value; | ||
| return; | ||
| } | ||
| } | ||
| keys[size] = key; | ||
| values[size] = value; | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public V get(K key) { | ||
| for (int i = 0; i < size; i++) { | ||
| if (Objects.equals(key, 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 equality check uses |
||
| return (V) 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 file imports and uses java.util.Objects which violates the checklist item: "Don't use class Objects". Replace
Objects.equals(...)with a null-safe equality check (or a private helper) and remove this import.