Skip to content
Snippets Groups Projects
ArrayListPerso.java 1.3 KiB
Newer Older
Thomas Lapp's avatar
Thomas Lapp committed
package p4a;
import java.util.Arrays;

public class ArrayListPerso<E> implements Structure{
	
	private E[] tableau;
	private int nbElements;
	private final static int taille_par_defaut = 10000;
	private final static int incr_par_defaut = 2500;
	
	
	public ArrayListPerso() {
		tableau = (E[]) new Object[taille_par_defaut];
	}
	
	@Override
	public void ajout(Object element, int position) {
		if (position < 0 || position > nbElements) {
			throw new IndexOutOfBoundsException("ajout : mauvais index " + position);
		}
		if (nbElements >= tableau.length) {
			tab = Arrays.copyOf(tableau, tableau.length + incr_par_defaut);
		}
		else {
			tab = tableau;
		}
		for (int i = nbElements; i > position; i--) {
			tab[i] = tab[i - 1];
		tab[position] = (E) element;
		tableau = tab;
		nbElements++;
		
	}
	@Override
	public void suppression(int position) {
		if (position < 0 || position >= nbElements) {
			throw new IndexOutOfBoundsException("suppression : position incorrecte " + position);
		}
		for (int i = position + 1; i < nbElements; i++) {
			tableau[i - 1] = tableau[i];
		}
		nbElements--;
		
	}
	@Override
	public Object acces(int position) {
		if (position > tableau.length || position < 0) {
			throw new IllegalArgumentException("acces : position incorecte");
		}
		return tableau[position];