2016-11-12 61 views
-1

这是一类学生一组操纵学生

public class Etudiant { 

    private int id; 
    private String name; 
    private String birthdate; 

    public Etudiant(int id,String name,String birthdate){ 
     this.id=id; 
     this.nom=name; 
     this.birthdate=birthdate; 
    } 
    public String getInfo(){ 
     return " "+ this.id+" "+this.name+" "+this.birthdate; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getbirthdate() { 
     return birthdate; 
    } 
    public void setbirthdate(String birthdate) { 
     this.birthdate=birthdate; 
    } 

我写了一个类Groupe操纵学生...... 但工作区的错误出现了,当我写我的main方法

这是我的代码:

import java.util.ArrayList; 
import java.util.Scanner; 

public class Groupe { 
    ArrayList <Student> etud = new ArrayList<Student>(); 
    private int id; 
    private String formation; 

    public Groupe(int id, String formation) { 
     this.id = id; 
     this.formation = formation; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getFormation() { 
     return formation; 
    } 

    public void setFormation(String formation) { 
     this.formation = formation; 
    } 

    public void ajouterEtudiant (Student e){ // add a student 
     Groupe g =new Groupe(this.id, this.formation); 
     g.etud.add(new Student(e.getId(), e.getName(), e.getbirthdate())); 
    } 
    public void supprimerEtudiant(Student e){ 
     Groupe g =new Groupe(id, formation); 
     if(!g.etud.isEmpty()){ 
      g.etud.remove(e); 
     }else{ 
      System.out.println("list empty "); 
     } 
    } 
    public void supprimerEtudiant(int id){ // delete a student using the id 
     int i=0; boolean B =true; 
     Groupe g = new Groupe(id, formation); 
     while ((i<g.etud.size())&&(B==true) &&(!g.etud.isEmpty())){ 
      if ((g.etud.get(i)).getId()==id){ 
       g.etud.remove(i); 
       B=false; 
      } 
      i++; 
     } 
    } 

    public void rechercheEtudiantNom(String name){ // search for a student by `enter code here`//his name 

     int i=0; 
     Groupe g =new Groupe(this.id, this.formation); 
     boolean B=true; 
     while ((i<g.etud.size())&& (B==true)&&(!g.etud.isEmpty())){ 
      if(g.etud.get(i).getname()==name){ 
       B=false; 
      } 
      i++; 
     } 
     if (B) { 
      System.out.println("student doesnt exist !"); 
     }else{ 
      System.out.println("student existe !"); 
     } 
    } 
    public void afficheTousEtudiant(){ // to show the list of students 
     Groupe g = new Groupe(this.id, this.formation); 
     for(int i = 0;i<g.etud.size()-1;i++){ 
      System.out.println(g.etud.get(i).getName()); 
     } 
    } 
} 

请告诉我我的方法中的错误在哪里。

+1

什么错误? – rafid059

+0

我没有找到错误..为什么我问....当你写这两个类..然后在MAIN例如创建一个群,然后你添加一个学生,然后你尝试按方法显示列表afficheTousEtudient()。 ...什么也没有显示 –

+0

发布您的主要类与主要方法 – rafid059

回答

0

在ajouterEtudiant()和supprimerEtudiant()中,您创建了一个新的Groupe实例,将其分配给g并在其中添加/删除该学生。这个'g'在你离开该方法后被丢弃。而不是直接使用实例的成员etud上该方法被称为:

public void ajouterEtudiant (Student e){ // add a student 
    etud.add(new Student(e.getId(), e.getName(), e.getbirthdate())); 
} 

BTW:该生产线

ArrayList <Student> etud = new ArrayList<Student>(); 

更好书面

List <Student> etud = new ArrayList<>(); 
+0

非常感谢........怎么样afficheTousEtudiant().......是对的! ?7 –

+0

这同样适用于您的所有方法。您永远不需要列表的新实例,始终只对您的Groupe实例的现有成员进行操作。 – Heri

+0

是的,我做到了......&我写了这个主要的主要{G =新集团(1,“SI”);学生e =新生(1,“mike”,“20-04-1990”); \t g.ajouterEtudiant(e); \t g.afficheTousEtudiant(); }但没有出现!!? –