Skip to content
Snippets Groups Projects
TableauPerso.java 1.26 KiB
Newer Older
Thomas Lapp's avatar
Thomas Lapp committed
package perso;

import structure.Structure;
public class TableauPerso implements Structure{
	
	private Object[] tableau;
	private int nbElements;
	public TableauPerso(int taille) {
		if (taille < 0) {
			throw new IllegalArgumentException("TableauPerso : taille negative " + taille);
		}
		
		this.tableau = new Object[taille];
		nbElements = 0;
	}

	@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 : tableau plein");
		}
		for (int i = nbElements; i > position; i--) {
			tableau[i] = tableau[i - 1];
		}
		tableau[position] = 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];
	}