Tableau java

Fermé
ozerus - 10 nov. 2011 à 15:02
KX Messages postés 16741 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 30 mai 2024 - 10 nov. 2011 à 16:45
Bonjour,

comment peut-on remplire avec java un tableau a plusieurs parametres. cad, t[i] contient plusieur objet, expl le nom, le prenom ....merci bien.
A voir également:

1 réponse

KX Messages postés 16741 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 30 mai 2024 3 016
10 nov. 2011 à 16:45
Tu dois créer une classe pour représenter tes données et faire un tableau d'objets de cette classe. Exemple :

class Personne
{
    protected final String prenom;
    protected final String nom;

    public Personne(String prenom, String nom)
    {
         this.prenom=prenom;
         this.nom=nom;
    }

    @Override
    public String toString()
    {
         return prenom+" "+nom;
    }
}

public class Test
{
    public static void main(String...args)
    {
        Personne[] tableau = {new Personne("Albert","Einstein"), new Personne("Galileo","Galilei")};

        for (int i=0; i<tableau.length; i++)
            System.out.println(tableau[i]);
    }
}
0