Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions data_structures/Lists/DynamicArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.*;

public class DynamicArray<E> {
private Object[] array;

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 int size;

public DynamicArray(int initialCapacity) {
this.array = new Object[initialCapacity];
this.size = 0;
}

private void resize() {
Object[] array = this.array;

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

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;

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 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)

Choose a reason for hiding this comment

The 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++)

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;

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.

return this.size;

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++)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use curly braces

v.add(i);

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 < 100; i++)
System.out.println(v.get(i));
}
}