Skip to content

Instantly share code, notes, and snippets.

@DaCurse
Created March 20, 2018 14:09
Show Gist options
  • Save DaCurse/54a34beea1550e9129968ad137aa7fd2 to your computer and use it in GitHub Desktop.
Save DaCurse/54a34beea1550e9129968ad137aa7fd2 to your computer and use it in GitHub Desktop.
DynamicArray in java
public class DynamicArray<T> {
protected Object[] items;
// Empty dynamic array
public DynamicArray() {
items = new Object[] {};
}
// Copy another dynamic array
public DynamicArray(DynamicArray<T> arr) {
items = arr.toArray();
}
// Use an array of T as the items
public DynamicArray(T[] items) {
this.items = items.clone();
}
// Returns the item at i position
public T get(int i) {
return (T) items[i];
}
// Sets them item at i position to the item that was passed
public void set(int i, T item) {
items[i] = item;
}
// Adds an item at the end of the array
public void add(T item) {
Object[] copy = new Object[items.length + 1];
System.arraycopy(items, 0, copy, 0, items.length);
copy[items.length] = item;
items = copy.clone();
}
// Removes the last item in the array and returns it
public T pop() {
Object[] copy = new Object[items.length - 1];
T last = (T) items[items.length - 1];
System.arraycopy(items, 0, copy, 0, items.length - 1);
items = copy.clone();
return last;
}
// Returns the index of the first occurrence of item
public int indexOf(Object item) {
int i = -1, counter = 0;
for (Object check : items) {
if (item.equals(check)) {
i = counter;
break;
}
counter++;
}
return i;
}
// Returns all indexes of the item
public int[] indexOfAll(Object item) {
int[] indexes = new int[] {};
int itemCounter = 0, indexCounter = 0;
for (Object check : items) {
if (item.equals(check)) {
int[] indexesCopy = new int[++indexCounter];
System.arraycopy(indexes, 0, indexesCopy, 0, indexes.length);
indexesCopy[indexes.length] = itemCounter;
indexes = indexesCopy.clone();
}
itemCounter++;
}
return indexes;
}
// Counts the amount of occurrences of an item
public int count(Object item) {
int counter = 0;
for (Object check : items) {
if (item.equals(check)) {
counter++;
}
}
return counter;
}
// Removes the item at the position i
public void remove(int i) {
Object[] copy = new Object[items.length - 1];
int itemCounter = 0, copyCounter = 0;
for (Object item : items) {
if (itemCounter++ != i) {
copy[copyCounter++] = item;
}
}
items = copy.clone();
}
// Removes all occurrences of item
public void removeAll(Object item) {
Object[] copy = new Object[items.length - count(item)];
int counter = 0;
for (Object check : items) {
if (!item.equals(check)) {
copy[counter++] = check;
}
}
items = copy.clone();
}
// Returns a new dynamic array that contains items from index start to end
// (including both)
public DynamicArray<T> chunk(int start, int end) {
Object[] copy = new Object[(end - start) + 1];
int counter = 0;
for (int i = start; i <= end; i++) {
copy[counter++] = items[i];
}
return new DynamicArray<T>((T[]) copy);
}
// Removes items from index start to end and returns a new dynamic array
// containing them
public DynamicArray<T> slice(int start, int end) {
DynamicArray<T> slice = chunk(start, end);
for (int i = 0; i <= (end - start); i++) {
remove(start);
}
return slice;
}
// Adds all items of another dynamic array at the end
public void merge(DynamicArray<T> items) {
for (T item : items.toArray()) {
add(item);
}
}
// Returns an array representation of the items
public T[] toArray() {
return (T[]) items.clone();
}
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
DynamicArray<Integer> arr = new DynamicArray<Integer>(new Integer[] { 1, 2, 3 });
DynamicArray<Integer> copy = new DynamicArray<Integer>(arr);
System.out.println("Initial arr: " + Arrays.toString(arr.toArray()));
System.out.println("Initial copy: " + Arrays.toString(copy.toArray()));
copy.set(1, 5);
System.out.println("Pop arr: " + arr.pop());
System.out.println("arr after pop: " + Arrays.toString(arr.toArray()));
System.out.println("Set item in copy: " + Arrays.toString(copy.toArray()));
arr.remove(0);
copy.add(3);
System.out.println("Remove from arr: " + Arrays.toString(arr.toArray()));
System.out.println("Add to copy: " + Arrays.toString(copy.toArray()));
System.out.println("Index of all 3s in copy: " + Arrays.toString(copy.indexOfAll(3)));
copy.removeAll(3);
System.out.println("Remove all 3s in copy: " + Arrays.toString(copy.toArray()));
arr.add(1);
arr.add(copy.get(0));
arr.add(10);
arr.add(copy.get(1));
System.out.println("Add 4 items to arr: " + Arrays.toString(arr.toArray()));
System.out.println("Chunk arr from index 1 to 3: " + Arrays.toString(arr.chunk(1, 3).toArray()));
copy.merge(arr);
System.out.println("Merge arr to copy: " + Arrays.toString(copy.toArray()));
System.out.println("Slice copy from index 1 to 3: " + Arrays.toString(copy.slice(1, 3).toArray()));
System.out.println("Copy after slicing: " + Arrays.toString(copy.toArray()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment