Skip to content
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
.settings
.project

.idea

# Mobile Tools for Java (J2ME)
.mtj.tmp/

Expand Down
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.

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>
154 changes: 154 additions & 0 deletions src/main/java/MyArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import java.util.Arrays;

public class MyArrayList<E> {

private int size;
private static final int DEFAULT_CAPACITY = 10;
private Object[] inputArray;


public MyArrayList() {
this(DEFAULT_CAPACITY);
}

public MyArrayList(int capacity) {
this.inputArray = new Object[capacity];
this.size = 0;
}

/**
* @return current number of elements in this list
*/
public int size() {
return size;
}

/**
* @return true if this list contains no elements
*/
public Boolean isEmpty() {
if (this.size == 0) {
return true;
}
return false;
}

/**
* Increases the capacity of the ArrayList instance, if necessary,
* to ensure that it can hold at least the number of elements
*/
private void ensureCapacity() {
if (size >= this.inputArray.length) {
int newCapacity = this.inputArray.length + DEFAULT_CAPACITY;
this.inputArray = Arrays.copyOf(this.inputArray, newCapacity);
}
}

/**
* Appends the specified element to the end of this list
*
* @param addMe element to be appended to this list
* @return true if appended and false if not appended
*/
public Boolean add(E addMe) {
ensureCapacity();
this.inputArray[size++] = addMe;
return true;
}

/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position(if any) and any subsequent
* elements to the right(adds 1 to the other indices).
*
* @param index at which specified element is to be inserted
* @param addMe element to be inserted to this list
* @return true if appended and false if not appended
*/
public Boolean add(int index, E addMe) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is too large or negative");
}
int length = size() - index;
ensureCapacity();
System.arraycopy(this.inputArray, index, this.inputArray, index + 1, length);
this.inputArray[index] = addMe;
return true;
}

/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left(subtracts one from their indices).
*
* @param index of element to be removed
* @return the element that was removed from the list
*/
@SuppressWarnings("unchecked")
public E remove(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is too large or negative");
}
E storePreviousElement = (E) this.inputArray[index];
int length = size() - index;
if (length > 0) {
System.arraycopy(this.inputArray, index + 1, this.inputArray, index, length);
this.inputArray[size--] = null;
}
return storePreviousElement;
}

/**
* Returns the element at the specified position in this list
*
* @param index of element to return
* @return element at the specified position in this list
*/
@SuppressWarnings("unchecked")
public E get(Integer index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is too large or negative");
}
return (E) this.inputArray[index];
}

/**
* Removes all of the elements from this list
* The list will be empty after this call returns.
*/
public void clear() {
this.inputArray = new Object[DEFAULT_CAPACITY];
this.size = 0;
}

/**
* @param element to be checked
* @return true if list contains the specified element.
*/
public Boolean contains(E element) {
for (Object obj : this.inputArray) {
if (obj != null && obj.equals(element))
return true;
}
return false;
}

/**
* Replaces the element at the specified position in this list with the specified element.
*
* @param index of the element to replace
* @param element to be stored at the specified position
* @return the element previously at the specified position
*/
@SuppressWarnings("unchecked")
public E set(int index, E element) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is too large or negative");
}
E storePreviousElement = (E) this.inputArray[index];
this.inputArray[index] = element;
return storePreviousElement;
}

}


Loading