vendredi 3 septembre 2010

java : prettyPrint

voici, une petite fonction pour afficher de jolie tableau :

ce qui donne par exemple, pour ce petit main :


public static void main(String[] args) {
// TODO Auto-generated method stub
String [] unSeul = {"6"};
String [] deux = {"6","3"};
String [] quatre = {"6","3","4","6"};

System.out.println("1 = " + prettyPrint(unSeul));
System.out.println("2 = " + prettyPrint(deux));
System.out.println("3 = " + prettyPrint(quatre));
}



Le résultat est le suivant :


1 = {6}
2 = {6,3}
3 = {6,3,4,6}



public String prettyPrint(String [] tab)
{
StringBuffer buffer = new StringBuffer("{");
if(tab != null)
{
int length = tab.length;
for(int i = 0; i < length; i++)
{
if((length > 2 && i != length - 1) || (length == 2 && i == 0))
{
buffer.append(tab[i] + ",");
}
else
{
buffer.append(tab[i]);
}
}
}
buffer.append("}");
return buffer.toString();
}

Aucun commentaire:

Enregistrer un commentaire