Skip to content

Commit

Permalink
initial commit, code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Yandrut committed Sep 20, 2024
0 parents commit 0cfff4b
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

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

7 changes: 7 additions & 0 deletions .idea/encodings.xml

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

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

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

124 changes: 124 additions & 0 deletions .idea/uiDesigner.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.

25 changes: 25 additions & 0 deletions pom.xml
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 src/main/java/org/yandrut/CustomList/CustomArrayList.java
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++];
}
}
}
Loading

0 comments on commit 0cfff4b

Please sign in to comment.