-
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
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,73 @@ | ||
import java.util.*; | ||
|
||
public class DynamicArray<E> { | ||
private Object[] array; | ||
private int size; | ||
|
||
public DynamicArray(int initialCapacity) { | ||
this.array = new Object[initialCapacity]; | ||
this.size = 0; | ||
} | ||
|
||
private void resize() { | ||
Object[] array = this.array; | ||
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. Use |
||
this.array = new Object[array.length * 2]; | ||
System.arraycopy(array, 0, this.array, 0, array.length); | ||
} | ||
|
||
public void add(E e) { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. You must add |
||
} | ||
|
||
public void add(int index, E e) { | ||
if (this.size == array.length) | ||
this.resize(); | ||
|
||
System.arraycopy(this.array, index, this.array, index + 1, this.size - index); | ||
this.array[index] = e; | ||
this.size++; | ||
} | ||
|
||
public E get(int index) { | ||
//noinspection unchecked | ||
return (E) this.array[index]; | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Use curly braces |
||
++this.size; | ||
else if (e == null && this.array[index] != null) | ||
--this.size; | ||
|
||
this.array[index] = e; | ||
} | ||
|
||
public int size() { | ||
return this.size; | ||
} | ||
|
||
public int capacity() { | ||
return this.array.length; | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Use curly braces |
||
if (this.array[i].equals(e)) | ||
return 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. Add comments in your code. |
||
return this.size; | ||
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. Why you don't return |
||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Use curly braces |
||
v.add(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. Added more test cases. For methodse |
||
for (int i = 0; i < 100; i++) | ||
System.out.println(v.get(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.
Why you don't use
E
instead ofObject[]