-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0cfff4b
Showing
13 changed files
with
640 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea/modules.xml | ||
.idea/jarRepositories.xml | ||
.idea/compiler.xml | ||
.idea/libraries/ | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.yandrut</groupId> | ||
<artifactId>CustomDataStructure</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>CustomDataStructure</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
130 changes: 130 additions & 0 deletions
130
src/main/java/org/yandrut/CustomList/CustomArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package org.yandrut.CustomList; | ||
import java.util.Iterator; | ||
|
||
public class CustomArrayList<T> implements CustomList<T> { | ||
private T[] myArrayList; | ||
private int size; | ||
|
||
public CustomArrayList() { | ||
myArrayList = (T[]) new Object[10]; | ||
size = 0; | ||
} | ||
|
||
@Override | ||
public void add(T t) { | ||
if (size == myArrayList.length) { | ||
T[] temporaryArray = (T[]) new Object[size + size / 3]; | ||
for (int i = 0; i < size(); i++) { | ||
temporaryArray[i] = myArrayList[i]; | ||
} | ||
myArrayList = temporaryArray; | ||
} | ||
myArrayList[size] = t; | ||
size++; | ||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
checkOutOfBounds(index); | ||
return myArrayList[index]; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return this.size; | ||
} | ||
|
||
@Override | ||
public boolean contains(Object toFind) { | ||
for (int i = 0; i < size; i++) { | ||
if (myArrayList[i].equals(toFind)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
checkOutOfBounds(index); | ||
T removedObject = myArrayList[index]; | ||
|
||
if (index == size - 1) { | ||
size--; | ||
} else { | ||
for (int i = index; i < size - 1; i++) { | ||
myArrayList[i] = myArrayList[i + 1]; | ||
} | ||
size--; | ||
} | ||
return removedObject; | ||
} | ||
|
||
@Override | ||
public boolean remove(Object object) { | ||
if (this.contains(object)) { | ||
remove(indexOf(object)); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int indexOf(Object object) { | ||
for (int i = 0; i < size; i++) { | ||
if (myArrayList[i].equals(object)) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return (size == 0); | ||
} | ||
|
||
@Override | ||
public T set(int index, T element) { | ||
if (index < 0 || index > size - 1) { | ||
throw new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + size); | ||
} | ||
myArrayList[index] = element; | ||
return element; | ||
} | ||
|
||
@Override | ||
public boolean addAll(CustomList<T> list) { | ||
if (list != null) { | ||
for (int i = 0; i < list.size(); i++) { | ||
this.add(list.get(i)); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
private void checkOutOfBounds(int index) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + size); | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterator() { | ||
return new MyIterator(); | ||
} | ||
|
||
private class MyIterator implements Iterator<T> { | ||
private int index; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return index < size(); | ||
} | ||
|
||
@Override | ||
public T next() { | ||
return myArrayList[index++]; | ||
} | ||
} | ||
} |
Oops, something went wrong.