Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_12.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

841 changes: 841 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions generics.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
179 changes: 179 additions & 0 deletions src/main/java/MyArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;

public class MyArrayList<E> {

private static final Logger logger = Logger.getGlobal();

private E[] myArrayList;
// where size = number of elements in array; size != array's length
private int size = 0;

public MyArrayList() {
this.myArrayList = (E[]) new Object[10];
}

public MyArrayList(Collection<? extends E> collection) {
this.myArrayList = (E[]) collection.toArray();
if (myArrayList.length == 0) {
size = 0;
} else {
for (int i = 0; i < myArrayList.length; i++) {
if (myArrayList[i] == null) {
size = i;
break;
}
}
}
}

public MyArrayList(int initialCapacity) throws IllegalArgumentException {
if (initialCapacity < 0) {
throw new IllegalArgumentException("The array's initial capacity cannot be less than zero.");
} else {
this.myArrayList = (E[]) new Object[initialCapacity];
}
}

public E[] getMyArrayList() {
return myArrayList;
}

public boolean add(E element) {
ensureCapacity();
myArrayList[size] = element;
size++;
return true;
}

public void add(int index, E element) {
if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
ensureCapacity();
if (myArrayList.length > 1) {
shiftElementsFromIndexToSize(index);
}
myArrayList[index] = element;
size++;
}

public E get(int index){
return myArrayList[index];
}

public E remove(int index) {
if (index < 0 || index >= myArrayList.length) {
if (capacity() == 0) {
logger.info("Array length is zero. There are no elements to delete in this array.");
} else {
logger.info("Index out of bounds: " + index);
}
throw new ArrayIndexOutOfBoundsException(index);
}
E elementRemoved = myArrayList[index];
if (! (index == size - 1)) {
shiftElementsFromSizeToIndex(index);
}
myArrayList[size - 1] = null;
size--;
return elementRemoved;
}

public void remove(E element) {
this.remove(this.indexOf(element)) ;
}

public E set(int index, E element) {
E elementPreviouslyAtIndex = myArrayList[index];
if (index < 0 || index >= myArrayList.length) {
throw new ArrayIndexOutOfBoundsException(index);
}
myArrayList[index] = element;
return elementPreviouslyAtIndex;
}

public void clear() {
this.myArrayList = (E[]) new Object[0];
this.size = 0;
}

public boolean isEmpty() {
if (myArrayList.length == 0) {
return true;
} else {
return false;
}
}

public boolean contains(E elementToFind) {
if (isEmpty()) {
return false;
}
for (E elementInList : myArrayList) {
if (elementInList == elementToFind) {
return true;
}
}
return false;
}

public int capacity() {
return myArrayList.length;
}

public int size() {
return size;
}

public void ensureCapacity() {
if (myArrayList.length == 0) {
myArrayList = (E[]) new Object[1];
} else if (size == myArrayList.length) {
doubleArrayCapacity();
}
}

public void doubleArrayCapacity() {
E[] arrayWithDoubleCapacity = (E[]) new Object[size * 2];
System.arraycopy(myArrayList, 0, arrayWithDoubleCapacity, 0, size);
myArrayList = arrayWithDoubleCapacity;
}

public void shiftElementsFromIndexToSize(int index) {
for (int i = size; i > index; i--) {
myArrayList[i] = myArrayList[i - 1];
}
}

public void shiftElementsFromSizeToIndex(int index) {
for (int i = index; i < size - 1; i++) {
myArrayList[i] = myArrayList[i + 1];
}
}

public Object[] toArray() {
return Arrays.copyOf(myArrayList, size);
}

public int indexOf(E element) {

if (element == null) {
for (int i = 0; i < this.myArrayList.length; i++) {
if (this.myArrayList[i] == element) {
return i;
}
}
}

for (int i = 0; i < this.myArrayList.length; i++) {
if (this.myArrayList[i].equals(element)) {
return i;
}
}

return -1;
}
}
93 changes: 93 additions & 0 deletions src/main/java/MySet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.util.Collection;
import java.util.Iterator;

public class MySet<E> {

private MyArrayList<E> myArrayList;

public MySet() {
this.myArrayList = new MyArrayList<>();
}

public MySet(Collection<? extends E> collection) {
this.myArrayList = new MyArrayList<>(collection);
}

public MySet(int initialCapacity) {
this.myArrayList = new MyArrayList<>(initialCapacity);
}

public boolean add(E element) {
if (myArrayList.isEmpty()) {
myArrayList.add(element);
} else if (! myArrayList.contains(element)) {
myArrayList.add(element);
}
return true;
}

// Could not get this method to work
// public boolean addAll(Collection<? extends E> collection) {
// for (E element : collection) {
// myArrayList.add(element);
// }
// return true;
// }

public void clear() {
this.myArrayList.clear();
}

public boolean contains(E elementToFind) {
return this.myArrayList.contains(elementToFind);
}

// public boolean containsAll(Collection<?> collection) {
// return false;
// }

// public boolean equals(Object object) {
// return false;
// }

// public int hashCode() {
// return 0;
// }

public boolean isEmpty() {
return this.myArrayList.isEmpty();
}

// public Iterator<E> iterator() {
// return null;
// }

public void remove(E element) {
this.myArrayList.remove(element);
}

// public boolean removeAll(Collection<?> collection) {
// return false;
// }

// public boolean retainAll(Collection<?> collection) {
// return false;
// }

public int size() {
return myArrayList.size();
}

public int capacity() {
return myArrayList.capacity();
}

public Object[] toArray() {
return myArrayList.toArray();
}

// public <T> T[] toArray(T[] a) {
// return a.toArray();
// }

}
Loading