-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
added solution #1590
base: master
Are you sure you want to change the base?
added solution #1590
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,71 @@ | |||||
import core.basesyntax.Storage; | ||||||
|
||||||
public class StorageImpl<K, V> implements Storage<K, V> { | ||||||
private static final int MAX_ARRAY_SIZE = 10; | ||||||
private final K[] keys; | ||||||
private final V[] values; | ||||||
private int currentSize; | ||||||
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.
Suggested change
|
||||||
|
||||||
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. Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the constructor. |
||||||
keys = (K[]) new Object[MAX_ARRAY_SIZE]; | ||||||
values = (V[]) new Object[MAX_ARRAY_SIZE]; | ||||||
currentSize = 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.
Suggested change
|
||||||
} | ||||||
|
||||||
@Override | ||||||
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. Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the |
||||||
public void put(K key, V value) { | ||||||
for (int i = 0; i < currentSize; 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. There is repeating code for checking if the key is already in the storage. Consider extracting this logic into a private method to adhere to the DRY principle. |
||||||
if (key == null && keys[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 comparison of keys using '==' does not account for the potential logical equality of key objects. It is better to use 'equals' method with a null check. |
||||||
values[i] = value; | ||||||
return; | ||||||
} | ||||||
} | ||||||
|
||||||
if (key == null) { | ||||||
if (currentSize < MAX_ARRAY_SIZE) { | ||||||
keys[currentSize] = null; | ||||||
values[currentSize] = value; | ||||||
currentSize++; | ||||||
} else { | ||||||
throw new IllegalStateException("Storage is full"); | ||||||
} | ||||||
return; | ||||||
} | ||||||
|
||||||
for (int i = 0; i < currentSize; i++) { | ||||||
if (keys[i] != null && keys[i].equals(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 null check for 'keys[i]' is not necessary here as it's already handled by the previous loop (lines 19-23). |
||||||
values[i] = value; | ||||||
return; | ||||||
} | ||||||
} | ||||||
|
||||||
if (currentSize < MAX_ARRAY_SIZE) { | ||||||
keys[currentSize] = key; | ||||||
values[currentSize] = value; | ||||||
currentSize++; | ||||||
} else { | ||||||
throw new IllegalStateException("Storage is full"); | ||||||
} | ||||||
} | ||||||
|
||||||
@Override | ||||||
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. Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the |
||||||
public V get(K key) { | ||||||
for (int i = 0; i < currentSize; 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. The same repeating code for checking if the key is already in the storage is found here as well. It's a good practice to create a private method for such repeated logic. |
||||||
if (key == null && keys[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. As mentioned before, consider using 'equals' method with a null check for comparing keys instead of '==', to ensure logical equality is considered. |
||||||
return values[i]; | ||||||
} | ||||||
} | ||||||
|
||||||
for (int i = 0; i < currentSize; i++) { | ||||||
if (keys[i] != null && keys[i].equals(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. Similar to the previous comment, the null check for 'keys[i]' is not necessary here as it's already handled by the previous loop (lines 54-58). |
||||||
return values[i]; | ||||||
} | ||||||
} | ||||||
return null; | ||||||
} | ||||||
|
||||||
@Override | ||||||
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. Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the |
||||||
public int size() { | ||||||
return -1; | ||||||
return currentSize; | ||||||
} | ||||||
} |
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.
Don't begin class or method implementation with an empty line. Please remove the redundant empty line at the beginning of the class definition.