package lab4;

import java.util.ArrayList;

/**
 * Tableau Dynamique lab 4 exercise 5
 * @author martin
 *Créez une classe TableauDynamique qui fera la même chose que la classe précédente, mais avec un
*ArrayList qui vous permettra de créer un tableau dynamique.
 */
public class TableauDynamique {
	/*properties*/
	private ArrayList<Integer> _tableau;

	/*constructor*/
	private TableauDynamique(ArrayList<Integer> tab){
		_tableau = tab;
	}

	/*static factory*/
	public static TableauDynamique fromSizeInitAtZero(int size){
		ArrayList<Integer> tmp = new ArrayList<Integer>();
		for (int i=0;i<size;i++)
			tmp.set(i, 0);
		return new TableauDynamique(tmp);
	}

	/*getter*/
	public int size(){
		return _tableau.size();
	}

	public int getAt(int pos){
		if (pos>=_tableau.size())
			throw new IndexOutOfBoundsException();
		return _tableau.get(pos).intValue();
	}


	/*setter*/
	public void setElement(int pos, int elem){
		if (pos>=_tableau.size())
			throw new IndexOutOfBoundsException();
		_tableau.set(pos, new Integer(elem));
	}

	/*mutator*/
	public void sort(){
		java.util.Collections.sort(_tableau);
	}

	/*method*/
	public void affiche(){
		for (int i=0;i<_tableau.size();i++)
			System.out.print(_tableau.get(i)+" ");
		System.out.println();
	}

	/*main*/
	public static void main(String [] args){
		Tableau tmp = Tableau.fromSizeInitAtZero(10);
		tmp.setElement(4,1);
		tmp.sort();
		tmp.affiche();
	}
}
