2016-09-22 94 views
1

我想实现一个ArrayList,它保存用户输入的细节并显示它们。代码工作正常,但构造函数从主要和从StudentDetails类的其他调用两次。有没有办法让它只打一次? 这里是Student类具有调用StudentDetails类的对象的主方法和具有ArrayList的StudentDetails类。构造函数被调用TWice

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());  
     } 
    } 
} 
+2

'StudentDetails SD1 =新StudentDetails()一个附加的类;','StudentDetails sDetails =新StudentDetails();'。是的,它被调用两次... – SomeJavaGuy

+0

提示:一个构造函数只能调用一次。将您的打印输出更改为包含* this *,并且您将意识到您的构造函数被称为“两次”,因为您正在创建**两个** StudentDetails对象。 – GhostCat

+0

我知道我正在创建两个对象。有没有一种解决方法可以使它与单个对象一起工作? –

回答

2

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

你可以通过使input()display()静态方法和改变sd静态变量避免在main创建对象的。

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() { 
    ... 
} 
+0

是的理解,并得到它的工作。 –

0

这里是更新的类。

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

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;   
     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)); 
      setNames(sc.next()); 
      System.out.println("Enter marks for same student"); 
      setMarks(sc.nextInt()); 
      sd.add(this); 

     } 
     sc.close(); 
    } 

    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()); 

     } 
    } 
} 
} 
0

作为@Eran's答案的一个选项,您可能希望采用更恰当的类设计。目前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; 
     } 

    } 
} 
+0

我觉得这两个setter方法在这里没有用处。 –