Skip to content
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
110 changes: 0 additions & 110 deletions Paquet.java

This file was deleted.

54 changes: 54 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.kaoutharelbakouri</groupId>
<artifactId>kaoutharelbakouri-implement-arraylist-from-scratch</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java Code to Implement an ArrayList From Scratch</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
97 changes: 97 additions & 0 deletions src/main/java/io/github/kaoutharelbakouri/Paquet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package io.github.kaoutharelbakouri;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class Paquet<T> implements Iterable<T> {
private int size;
private T[] tab;

public Iterator<T> iterator() {
return new Iterator<T>() {
int currentIndex = 0;

@Override
public boolean hasNext() {
return currentIndex < size;
}

@Override
public T next() {
if (!hasNext()) throw new NoSuchElementException();
return tab[currentIndex++];
}

@Override
public void remove() {
Paquet.this.remove(--currentIndex);
}
};
}

@SuppressWarnings("unchecked")
public Paquet() {
size = 0;
tab = (T[]) new Object[10];
}

public void add(T e) {
if (size == tab.length) {
tab = Arrays.copyOf(tab, size * 2 + 1);
}
tab[size++] = e;
}

public void add(int index, T item) {
checkArrayIndexOutOfBounds(index);
if (size == tab.length) {
tab = Arrays.copyOf(tab, size * 2 + 1);
}
for (int i = size; i > index; i--) {
tab[i] = tab[i - 1];
}
tab[index] = item;
size++;
}

public void remove(int index) {
checkArrayIndexOutOfBounds(index);
for (int i = index; i < size - 1; i++) {
tab[i] = tab[i + 1];
}
size--;
}

public int getSize() {
return size;
}

public T get(int index) {
checkArrayIndexOutOfBounds(index);
return tab[index];
}

public String toString() {
StringBuilder s = new StringBuilder("Les elements de notre Collection sont :");
for (T t : this) {
s.append(" ").append(t);
}
return s.toString();
}

public boolean isEmpty() {
return size == 0;
}

public void set(int index, T newVal) {
checkArrayIndexOutOfBounds(index);
tab[index] = newVal;
}

private void checkArrayIndexOutOfBounds(int index) throws ArrayIndexOutOfBoundsException {
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException(index + " is out of Bounds");
}
}
}
Loading