Skip to content

finished #17

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.

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

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Implement your own version of an ArrayList (we'll call it MyArrayList), with the
- MyArrayList must be a generic type, which can store any other type of object
- MyArrayList must not use List, ArrayList or any other collection type in its implementation, only Arrays (The purpose of this is to practice designing your own generic class from the ground up, so that you can appreciate their complexities and usefulness)
- MyArrayList must have `add()`, `get()`, `remove()`, `set()`, `clear()`, `isEmpty()`, `contains()` and other methods to provide the same basic functionality described in the [ArrayList documentation](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html)
- `add()` should be overridden to add an element to the end of the Array, or to add the element to a specified index in the array.
- `add()` should be overridden to add an element to the end of the Array, or to add the element to a specified myLength in the array.
- MyArrayList must have a no-argument constructor (`MyArrayList()`) to initialize a new instance, it should also have a one-argument constructor (`MyArrayList(int)`) that takes an int and starts the list off at the specified size.
- MyArrayList should automatically resize and maintain its order.

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

public class MyArrayList <E> {
//NO use of List, ArrayList, or any Collection, use ONLY Arrays
private E[] myArrayList;
private Integer size = 0;
private final Integer DEFAULTINCREMENTOR = 10;

//Create a empty list with size of the parameter
public MyArrayList (int length){
myArrayList = (E[]) new Object[length];
}

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

public Integer size() {
return this.size;
}


public void add(E element){
if (size() < myArrayList.length){
myArrayList[size()]=element;
}
else if(size() == myArrayList.length){
E[] newArray = Arrays.copyOf(myArrayList, myArrayList.length+this.DEFAULTINCREMENTOR );
newArray[size()+1]=element;
myArrayList = newArray;
}
this.size++;
}

public void add(int index, E element){
if (size() < myArrayList.length){
for(int i = index - 1; i > 0; i--) {
myArrayList[i] = myArrayList[i - 1];
}
myArrayList[index]=element;
}
else if(size() == myArrayList.length){
E[] newArray = Arrays.copyOf(myArrayList, myArrayList.length+this.DEFAULTINCREMENTOR );
for(int i = index - 1; i > 0; i--) {
myArrayList[i] = myArrayList[i - 1];
}
newArray[index]=element;
myArrayList = newArray;
}
this.size++;
}

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

public Integer indexOf (E object){
for (int i = 0; i < size(); i++) {
if(myArrayList[i].equals(object)){
return i;
}
}
return -1;
}
public void remove(int index) {
E[] newArray = Arrays.copyOf(myArrayList, myArrayList.length-1 );
myArrayList[index] = null;
int newArrIndex = 0;
for (int i = 0; i <size() ; i++) {
if (myArrayList[i] != null) {
newArray[newArrIndex] = myArrayList[i];
newArrIndex++;
}
}
myArrayList = newArray;
this.size--;

}

public void set(int index, E element){
myArrayList[index] = element;
}
public void clear() {
for (int i = 0; i < size(); i++) {
myArrayList[i] = null;
}
this.size = 0;

}

public boolean isEmpty() {
if(size() < 1){
return true;
}
return false;
}

public boolean contains (E object){
for (int i = 0; i <size() ; i++) {
if(myArrayList[i].equals(object)){
return true;
}
}
return false;

}

public String printOut(){
StringBuilder sb = new StringBuilder();
for (int i = 0; i <size ; i++) {
sb.append(myArrayList[i]+ " ");
}
return sb.toString();
}
}
8 changes: 8 additions & 0 deletions src/main/java/MyMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class MyMap<K, V> {

private MySet<K> myMap;

private Integer size = 0;


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

public class MySet<E> {

//
private MyArrayList<E> mySet;
private Integer size = 0;

//Create a empty list with size of the parameter
public MySet (int length){
mySet = new MyArrayList<>(length);
}

public MySet() {
mySet = new MyArrayList<>();
}

public Integer size() {
return this.size;
}


public void add(E element){
if(!mySet.contains(element)){
mySet.add(element);
size++;
}
}

public E get(Integer index) {
return mySet.get(index);
}

public Integer indexOf (E object){
return mySet.indexOf(object);
}
public void remove(int index) {
mySet.remove(index);

}

public void set(int index, E element){
mySet.set(index, element);
}
public void clear() {
mySet.clear();
size = 0;
}

public boolean isEmpty() {
return mySet.isEmpty();
}

public boolean contains (E object){
return mySet.contains(object);

}
}
Loading