Skip to content
Snippets Groups Projects
Forked from GOSSA JULIEN / P4a
8 commits ahead of the upstream repository.
ArrayListPerso.java 1.18 KiB
package p4a;

public class ArrayListPerso<E> implements Structure{
	
	private E[] tableau;
	private int nbElements;
	private final static int taille_par_defaut = 1000;
	
	
	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) {
			throw new IllegalStateException("ajout : ArrayListPerso pleine");
		}
		for (int i = nbElements; i > position; i--) {
			tableau[i] = tableau[i - 1];
		}
		tableau[position] = (E) element;
		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];
	}
	
	
	
	
}