IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Collection et Stream Java Discussion :

Probleme copie d'objet dans un tableau


Sujet :

Collection et Stream Java

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2003
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 7
    Points : 6
    Points
    6
    Par défaut Probleme copie d'objet dans un tableau
    Bonjour,

    j'ai réaliser cette méthode:
    Dans la classe Etudiant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static int addeleve(Etudiant etd){
        if (i<student.length | l<perso.length){//test de tableau perso et etudiant atteint
        student[i]=etd; //je met l'etudiant dans le tableau cellule indice i
        i=i+1;   //j'avance i de 1
    
        }
        else{
               System.out.println(" impossible d'ajouter l'Etudiant : maximum atteint");
            }
        return i;
    dans le programme principale :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    compte=0;
     
            for(compte=0;compte <Etudiant.i;compte++){
             System.out.println("Etudiant"+compte+" : "+Etudiant.student[compte].unetudiant.unepersonne.nom + " "+Etudiant.student[compte].unetudiant.unepersonne.prenom+" "+Etudiant.student[compte].unetudiant.unepersonne.email+" "+Etudiant.student[compte].unetudiant.note);
     
            }

    Donc on devrait afficher les etudiants qui ont ete mis dans le tableau, mais la ça ne marche pas le tableau est rempli uniquement par le dernier étudiant :

    TEST

    zzz aaa //creation personne
    Etudiant: zzz aaa 10.0 // etudiant extend personne
    //etudiant 1 indice 0 du tableau

    FGHFGHFGH qsdsdghjhjhkjk //creation personne
    Etudiant: FGHFGHFGH qsdsdghjhjhkjk 78.0
    //etudiant 2 indice 1 du tableau

    quand on demande le listage du tableau :
    Etudiant0 : FGHFGHFGH qsdsdghjhjhkjk 78.0
    Etudiant1 : FGHFGHFGH qsdsdghjhjhkjk 78.0

    Comme si la dernière valeur avait écrasé les autres...
    l'etudiant 1 n'apparait pas à l'indice 0 !!!

    Merci de votre aide

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Août 2006
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 52
    Points : 55
    Points
    55
    Par défaut
    Bonsoir,

    Ton message est incompréhensible !!

    De ce que j'ai pu comprendre, ils sortent d'ou ton i et ton l dans addEleve ???

    Serait-il possible d'avoir un peu plus de code?

  3. #3
    Membre chevronné
    Profil pro
    Fabrication GED
    Inscrit en
    Octobre 2005
    Messages
    1 405
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Fabrication GED

    Informations forums :
    Inscription : Octobre 2005
    Messages : 1 405
    Points : 1 958
    Points
    1 958
    Par défaut
    Avant de citer ton code, tu précises que c'est dans ta classe étudiant, or la méthode citée est addEleve(Etudiant etd). Cela veut dire que chaque étudiant possède un tableau d'étudiant ??!!
    La c'est un problème d'expression ou un problème de conception car je doute que dans ton exercice, il y ait des liens entre étudiants, notament pour la gestion de notes

    Maintenant pour ton problème d'étudiants identiques, je parie que c'est un problème de références. Montre-nous comment tu appelles ta méthode "add".
    Remarque, je sais pas pourquoi mais je sens venir le coup d'instance d'étudiant déclaré en static dans les attributs de classe - coup hyper classique du débutant holé-holé qui voit qu'en rajoutant des "static" partout, ça compile mais sans comprendre pourquoi.

    et +1 c'est incompréhensible.

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2003
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 7
    Points : 6
    Points
    6
    Par défaut oui en effet je débute :-)
    Bonjour,
    merci pour vos réponses
    1-creation d'un objet Personnel
    2-Crétion d'un objet Etudiant (qui s'appel unetudiant) qui hérite de personnel
    3-Chaque fois qu'un etudiant est ajouté je veux le mettre dans un tableau par la méthode asddeleve
    Mon probleme : si je rentre par exemple 10 etudiants différents dans mon tableau, au final dans le tableau quand je le liste j'ai 10 fois le dernier etudiant entré.

    iohack je crois que tu es dans le vrai, je te remercie de ton aide. Au sujet de ce static je ne comprends pas bien...son utilité malgrès pas mal de recherche :-)

    l'appel d'etudiant dans le prog principale :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:
     
            String thename = jTextField1.getText();     //on met le contenu du jtextfield1 dans  la var thename
            String firstname= jTextField2.getText();    //on met le contenu du jtextfield2 dans  la var firstname
            String themail=jTextField3.getText();       //on met le contenu du jtextfield dans  la var themail
            Personnel.constperso(thename,firstname,themail);        //on evoi le tout vers constperso() dans personnel
     
            /***************ICI on affiche dans la console  le nom,prenom email de 
             *               la personne pour vérification constructeur*********/
            System.out.println(Personnel.unepersonne.nom + " "+Personnel.unepersonne.prenom+" "+Personnel.unepersonne.email);
     
            String strnote = JOptionPane.showInputDialog(null,"quelle note ?"); //on récupére la note en sting
            float lanote;   //on cree une variable type float pour la note
            lanote = Float.valueOf(strnote); //conversion de la note en float
            Etudiant.constetud(Personnel.unepersonne,lanote);
     
            /***************ICI on affiche dans la console  le nom,prenom,emailet note  de 
             *               l'etudiant pour vérification constructeur*********/
            System.out.println("Etudiant: "+Etudiant.unetudiant.unepersonne.nom + " "+Etudiant.unetudiant.unepersonne.prenom+" "+Etudiant.unetudiant.unepersonne.email+" "+Etudiant.unetudiant.note);
            Personnel.addpersonne(Personnel.unepersonne);
            Etudiant.addeleve(Etudiant.unetudiant);          
     
        }
    la déclaration des variables dans le prog principale
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
     
    // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JButton jButton6;
        private javax.swing.JButton jButton7;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        public static javax.swing.JTextField jTextField1;
        public static javax.swing.JTextField jTextField2;
        public static javax.swing.JTextField jTextField3;
        // End of variables declaration
    voici la classe etudiant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    public class Etudiant extends Personnel{    //sous classe etudiant de Personnel
        String nom;
        String prenom;
       static String email;
        float note; 
        static Etudiant unetudiant;
        static Etudiant student[]=new Etudiant[10]; //maximum 10 etudiants
        public static int i=0;   //pour etudiant 
    public static void constetud (Personnel unepersonne,float entnote){        
            if (unepersonne.email==null|unepersonne.email==""){
                unepersonne.email="pas d email";
     
                unetudiant = new Etudiant() ; // constructeur
             unetudiant.nom=unepersonne.nom.toUpperCase();
             unetudiant.prenom=unepersonne.prenom;
             unetudiant.email=unepersonne.email;
             unetudiant.note=entnote;
             unepersonne = unetudiant;
            } 
     
     
            else {
              unetudiant = new Etudiant() ; 
             unetudiant.nom=unepersonne.nom.toUpperCase();
             unetudiant.prenom=unepersonne.prenom;
             unetudiant.email=unepersonne.email;
             unetudiant.note=entnote;
             unepersonne = unetudiant;
           } 
            }
    public static int addeleve(Etudiant etd){ 
        if (i<student.length & l<perso.length){//test de tableau perso et etudiant atteint
        student[i]=etd;
        i=i+1;
     
        }
        else{
               System.out.println(" impossible d'ajouter l'Etudiant : maximum atteint"); 
            }
        return i;
    }
     
    }
    Voici la classe personnel :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    public class Personnel {
     
     
                String nom;
                String prenom;
                String email;
            static   Personnel unepersonne ; 
            static Personnel perso[]=new Personnel[15]; //maximum 15 personnes 
             public static int l=0;   //personnel
     public static void constperso (String entnom,String entprenom,String entemail){        
            if (entemail==""|entemail==null){
               entemail="pas d email"; 
               mailvide(entnom,entprenom,entemail);
     
            } 
     
     
            else {
     
             avecmail(entnom,entprenom,entemail);
           } 
     
     
            }
     public static void mailvide(String entnom,String entprenom,String entemail){
     
         unepersonne = new Personnel() ; 
     
           unepersonne.nom=entnom.toUpperCase();
             unepersonne.prenom=entprenom;
               unepersonne.email=entemail;
     }
     
      public static void avecmail(String entnom,String entprenom,String entemail){
         unepersonne = new Personnel() ; 
     
             unepersonne.nom = entnom.toUpperCase();
             unepersonne.prenom = entprenom;
            unepersonne.email = entemail;
     }
     public static void addpersonne (Personnel uneper){ 
        if (l<perso.length){//test de tableau perso atteint
        perso[l]=uneper;
        l=l+1;//avance du tableau de personnel soit perso
        }
        else{
               System.out.println(" impossible d'ajouter la personne : maximum atteint"); 
            }
    }
     }
     
     
    //public void affichpers(String entnom, String entprenom, String entemail,String entstatut){
        //dans les parametres manque le num de la persone a afficher
       // System.out.println(nom, prenom, email, statut);
     
    //}


    Désolé pour la longueur
    Je vous remercie par avance.
    Ché

Discussions similaires

  1. Rechercher un objet dans un tableau d'objet
    Par mikaelm dans le forum Ruby
    Réponses: 6
    Dernier message: 11/06/2007, 17h58
  2. copie de texte dans un tableau c++
    Par snoopgod4 dans le forum C++
    Réponses: 5
    Dernier message: 09/03/2007, 11h38
  3. Réponses: 1
    Dernier message: 05/03/2007, 10h39
  4. [Debutant] Stocker des objets dans un tableau à plusieurs indices
    Par Invité dans le forum Collection et Stream
    Réponses: 4
    Dernier message: 27/09/2006, 18h04
  5. Problème avec un background dans un tableau
    Par Space Cowboy dans le forum Balisage (X)HTML et validation W3C
    Réponses: 25
    Dernier message: 08/08/2006, 23h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo