Skip to content

Turning In all 3 parts done #9

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 11 commits 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.

784 changes: 784 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>
Empty file removed src/main/java/.deleteme
Empty file.
45 changes: 45 additions & 0 deletions src/main/java/Mapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.Objects;

public class Mapping<K,V> {

K key;
V value;

public Mapping(K key, V value){
this.key = key;
this.value = value;

}

public V getValue(){
return this.value;
}

public K getKey(){
return this.key;
}

public void setValue(V newValue){
this.value = newValue;

}

@Override
public boolean equals(Object o){
if(o==this)return true;
if(!(o instanceof Mapping))return false;

Mapping m = (Mapping) o;
return this.key.equals(m.key);

}

/*@Override
public int hashCode(){
return Objects.hash(key,value);
}*/




}
245 changes: 245 additions & 0 deletions src/main/java/MyArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@


import java.util.Arrays;

@SuppressWarnings("unchecked")
public class MyArrayList<T> {



private T[] myArray;
private int numberOfObjects;



public MyArrayList(){
this.myArray = (T[]) new Object[0];

}

public MyArrayList(int capacity){
this.myArray = (T[]) new Object[capacity];

}

public void add(T element){
if(this.contains(null)){
this.set(this.indexOf(null), element);
}
else{
this.myArray = Arrays.copyOf(myArray, myArray.length+1);
this.myArray[this.myArray.length-1] = element;
}
this.numberOfObjects++;
}

public void add(int index, T element){
if(this.myArray[index] == null){
this.myArray[index] = element;
this.numberOfObjects++;
}

else if(this.contains(null)){
this.myArray = Arrays.copyOf(myArray, this.myArray.length);
this.shiftValuesAndReplaceTargets(index,element);
}

else{
this.myArray = Arrays.copyOf(myArray, this.myArray.length+1);
this.shiftValuesAndReplaceTargets(index,element);
}
}

public void addAll(T[] elements){
if(this.contains(null)){
this.myArray = Arrays.copyOf(myArray, this.newArraySize(elements));
int counter = this.indexOf(null) ;
for(int i = 0 ; i<elements.length; i++){
this.myArray[counter] = elements[i];
counter++;
this.numberOfObjects++;
}
}
else{
for(T t : elements){
this.add(t);
}
}
}

public void addAll(int index, T[] elements){
if(this.myArray[index] == null && elements.length <= (this.myArray.length-this.size())){
int counter = index;
for(int i = 0; i< elements.length; i++) {
this.myArray[counter] = elements[i];
this.numberOfObjects++;
}
}

else if(this.contains(null)){
this.myArray = Arrays.copyOf(myArray, newArraySize(elements));
shiftValuesAndReplaceTargets(index,elements);
}

else{
this.myArray = Arrays.copyOf(myArray, newArraySize(elements));
shiftValuesAndReplaceTargets(index,elements);
}

}

public void clear(){
for(int i = 0; i<this.myArray.length; i++){
this.myArray[i] = null;
}
this.numberOfObjects=0;

}

public boolean contains(T element){
if (element == null){
for (T t : myArray){
if(t == null) return true;
}
}

for (T t : myArray){
if(t == null) continue;
else if (t.equals(element)) return true;
}
return false;
}

public void set(int index, T element){
this.myArray[index] = element;

}

public T get(int index){
return this.myArray[index];
}

public int indexOf(T element){

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

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

public boolean isEmpty(){

return this.size() == 0;
}

public int lastIndexOf(T element){
for(int i = this.myArray.length-1; i>=0; i--){
if (this.myArray[i].equals(element)){
return i;
}
}
return -1;
}

public void remove(int index){
T[] tempArray = (T[]) new Object[0];
for(int i = 0; i<this.myArray.length; i++){
if(i != index){
tempArray = Arrays.copyOf(tempArray,tempArray.length+1);
tempArray[tempArray.length-1] = this.myArray[i];
}
}
tempArray = Arrays.copyOf(tempArray,tempArray.length+1);
this.myArray = tempArray;
this.numberOfObjects -= 1;
}

public void remove(T object){
this.remove(this.indexOf(object));

}

public void removeRange(int startIndex, int endIndex){
T[] tempArray = (T[]) new Object[0];
for(int i = 0; i<this.myArray.length; i++){
if (i < startIndex || i > endIndex){
tempArray = Arrays.copyOf(tempArray,tempArray.length+1);
tempArray[tempArray.length-1] = this.myArray[i];
}
}
tempArray = Arrays.copyOf(tempArray,tempArray.length+(endIndex-startIndex)+1);
this.myArray = tempArray;
this.numberOfObjects -= (endIndex - startIndex)+1;

}

public int size(){
return this.numberOfObjects;
}

public T[] subArray(int startIndex, int endIndex, T[] newArray){
T[] tempArray = (T[]) new Object[0];
int counter = 0;
for(int i = startIndex; i <= endIndex; i++){
tempArray = Arrays.copyOf(tempArray,tempArray.length+1);
tempArray[counter] = this.myArray[i];
counter++;
}
return (T[]) Arrays.copyOf(tempArray, tempArray.length, newArray.getClass());
}

public T[] toArray(T[] newArray){

return (T[]) Arrays.copyOf(this.myArray, this.myArray.length, newArray.getClass());
}

public void trimToSize(){
this.myArray = Arrays.copyOf(this.myArray, this.size());

}

private int countNulls(){
int counter = 0;
for (T t : this.myArray){
if (t == null){
counter++;
}
}
return counter;
}

private int newArraySize(T[] input){
if(this.countNulls() >= input.length){
return this.myArray.length;
}
else{
return this.myArray.length + (input.length-this.countNulls());
}
}

private void shiftValuesAndReplaceTargets(int index, T ... elements){
int count = index;
T[] tempArray = Arrays.copyOf(myArray, myArray.length);
for(int i = (index+elements.length); i<tempArray.length; i++){
tempArray[i] = this.myArray[i-elements.length];
}

for(int i = 0; i<elements.length; i++) {
tempArray[count] = elements[i];
count++;
this.numberOfObjects++;
}
this.myArray = tempArray;
}

}
Loading