-
Notifications
You must be signed in to change notification settings - Fork 20k
Dynamic Array #271
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
Dynamic Array #271
Conversation
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.
Good Work! I have some requests.
import java.util.*; | ||
|
||
public class DynamicArray<E> { | ||
private Object[] array; |
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.
Why you don't use E
instead of Object[]
} | ||
|
||
private void resize() { | ||
Object[] array = this.array; |
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.
Use tmp
instead of array
. Because array
can be confusing.
Why you don't use E
instead of Object
if (this.size == array.length) | ||
this.resize(); | ||
|
||
this.array[this.size++] = e; |
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.
You must add this.size++
} | ||
|
||
public void set(int index, E e) { | ||
if (this.array[index] == null && e != null) |
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.
Use curly braces
} | ||
|
||
public int indexOf(E e) { | ||
for (int i = 0; i < this.size; i++) |
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.
Use curly braces
if (this.array[i].equals(e)) | ||
return i; | ||
|
||
return this.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.
Why you don't return -1
?
public static void main(String[] args) { | ||
DynamicArray<Integer> v = new DynamicArray<>(11); | ||
|
||
for (int i = 0; i < 100; i++) |
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.
Use curly braces
|
||
for (int i = 0; i < 100; i++) | ||
v.add(i); | ||
|
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.
Added more test cases. For methodse set
and add(index,e)
.
for (int i = 0; i < this.size; i++) | ||
if (this.array[i].equals(e)) | ||
return i; | ||
|
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.
Add comments in your code.
Issue #96