2016-09-22 129 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方法在這裏沒有用處。 –