package perso; import java.util.Arrays; import structure.Structure; 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) { E[] tab; 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]; } }