构造函数被称为TWice

时间:2016-09-22 13:20:32

标签: java

我正在尝试实现一个ArrayList,它保存用户输入的详细信息并显示它们。代码工作正常,但构造函数从main调用两次,而StudentDetails类调用另一次。有没有办法让它只召唤一次? 以下是具有StudentDetails类的主方法调用对象的Student类和具有ArrayList的StudentDetails类。

public class Student2 {      

  public static void main(String[] args) {              
     StudentDetails sd1 = new StudentDetails();
     sd1.input();
     sd1.display();        
  }

  class StudentDetails {    
    int marks;
    String names;
    List<StudentDetails> sd = new ArrayList<>();

    public int getMarks() {
        return marks;
    }

    public void setMarks(int marks) {
        this.marks = marks;
    }

    public String getNames() {
        return names;
    }

    public void setNames(String names) {
        this.names = names;
    }

    public StudentDetails() {    
        System.out.println("Program Started");
    }

    public void input() {    
        int no;
        StudentDetails sDetails = new StudentDetails();
        System.out.println("How many students?");
        Scanner sc = new Scanner(System.in);
        no = sc.nextInt();

        for (int i = 0; i < no; i++) {
            System.out.println("Enter name of student" + (i + 1));
            sDetails.setNames(sc.next());
            System.out.println("Enter marks for same student");
            sDetails.setMarks(sc.nextInt());
            sd.add(sDetails);    
        }    
    }

    public void display() {
        for (int i = 0; i < sd.size(); i++) {                
            System.out.println("The name of student" + " " + (i + 1) + " " + "is" + " " + sd.get(i).getNames()
                    + " and marks are" + " " + sd.get(i).getMarks());    
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您正在调用它两次(创建两个StudentDetails个实例),实际上这还不够。您的input()方法应该多次调用它 - 每次迭代循环一次 - 因为您要将这些对象添加到List中,并且您不想多次添加同一个对象。

您可以通过制作maininput()静态方法并将display()更改为静态变量来避免在sd创建对象。

public static void main(String[] args) {              
   StudentDetails.input();
   StudentDetails.display();        
}

...
static List<StudentDetails> sd = new ArrayList<>();
...
public static void input() {
    ...
    for (int i = 0; i < no; i++) {
        StudentDetails sDetails = new StudentDetails();
        System.out.println("Enter name of student" + (i + 1));
        sDetails.setNames(sc.next());
        System.out.println("Enter marks for same student");
        sDetails.setMarks(sc.nextInt());
        sd.add(sDetails);    
    } 
    ...
}

public static void display() {
    ...
}

答案 1 :(得分:0)

这是更新的课程。

2

答案 2 :(得分:0)

作为@Eran答案的一个选项,您可能希望采用更合适的类设计。目前,List<StudentDetails>取决于StudentDetails的一个实例,在我看来,这个实例在开始时没有意义。

创建一个额外的类,充当StudenDetails

的管理器
public class Student2 {

    public static void main(String[] args) {
        // We create a Dictionary here now. This holds the StudentDetails now
        StudenDictionary sd1 = new StudenDictionary();
        sd1.input();
        sd1.display();
    }

    static class StudenDictionary {
        List<StudentDetails> sd = new ArrayList<>();

        static Scanner sc = new Scanner(System.in);

        public void input() {
            int no;

            System.out.println("How many students?");
            no = sc.nextInt();

            for (int i = 0; i < no; i++) {
                System.out.println("Enter name of student" + (i + 1));
                // Store in variables and use a proper constructor
                String names = sc.next();
                System.out.println("Enter marks for same student");
                int marks = sc.nextInt();
                // StudenDetails variable in loop now, caring for scope now
                StudentDetails sDetails = new StudentDetails(names, marks);
                sd.add(sDetails);
            }
        }

        public void display() {
            for (int i = 0; i < sd.size(); i++) {
                System.out.println("The name of student" + " " + (i + 1) + " " + "is" + " " + sd.get(i).getNames()
                        + " and marks are" + " " + sd.get(i).getMarks());
            }
        }
    }

    static class StudentDetails {
        int marks;
        String names;

        public int getMarks() {
            return marks;
        }

        public void setMarks(int marks) {
            this.marks = marks;
        }

        public String getNames() {
            return names;
        }

        public void setNames(String names) {
            this.names = names;
        }

        // Use a proper constructor
        public StudentDetails(String names, int marks) {
            this.names = names;
            this.marks = marks;
        }

    }
}