2017-03-01 114 views
0

我已經使用輸入來獲取值並將其添加到ArrayList。當我第二次輸入數據時,它會在輸入第二個數據後覆蓋第一個數據。無法修復它。如何停止覆蓋數組列表?

Student類

public class Student { 
static int rollNumber, age; 
static String firstName, lastName, gender; 
public Student(int rollNumber, String firstName, String lastName, int age, String gender) 
{ 
    this.rollNumber = rollNumber; 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.age = age; 
    this.gender = gender; 
    } 
} 

InputParticipant類

import java.util.*; 
public class InputParticipant extends Student{ 
     public static String sportsEvent; 
     public InputParticipant(int rollNumb, String firstName, String lastName, 
     int age, String gender, String sportsEvent) 
     { 
      super(rollNumber, firstName, lastName, age, gender); 
      this.sportsEvent=sportsEvent; 
     } 
     public void elegibility() 
     { 
      System.out.print("Eligibility:"); 
      //50 meter race 
      if(age>=10 && age<=12 && gender.equals("female") && sportsEvent.equals("50meterrace")) 
      { 
       System.out.println(" Yes "); 
      } 
      else if((age>=10 && age<=15) && sportsEvent.equals("100meterrace"))//100 meter race 
      { 
       System.out.println(" yes "); 
      } 
      else if(age>=15 && (sportsEvent.equals("400meterrace") || sportsEvent.equals("Juvelin") || sportsEvent.equals("discus")))//400 meter race, Juvelin and discus 
      { 
       System.out.println(" Yes "); 
      } 
      else 
      { 
       System.out.println(" No (age:Min 10-12 for 50m race-female only, Min 10-15 for 100m race and Min 15 for 400m race, Juvelin and discus)"); 
      } 

     } 
     public static void main(String[] args) 
     { 
      List<InputParticipant> stud=new ArrayList<InputParticipant>(); 
      InputParticipant i; 
      i=new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent); 
      Scanner in=new Scanner(System.in); 
      while(true) 
      { 
       System.out.println("Enter the Student details:"); 
       System.out.print("roll number:"); 
       rollNumber=in.nextInt(); 
       System.out.print("First name:"); 
       firstName=in.next(); 
       System.out.print("Last name:"); 
       lastName=in.next(); 
       System.out.print("age:"); 
       age=in.nextInt(); 
       System.out.print("gender:"); 
       gender=in.next(); 
       System.out.print("sports event(50meterrace/100meterrace/400meterrace/Juvelin/discus):"); 
       sportsEvent=in.next(); 
       stud.add(i); 
       List<InputParticipant> l=new ArrayList<InputParticipant>(); 
       l.addAll(stud); 
       Iterator<InputParticipant> disp= l.iterator(); 
       while(disp.hasNext()) 
       { 
        InputParticipant st=disp.next(); 
        System.out.println("Roll Number:"+st.rollNumber+"\nName: "+st.firstName+" "+st.lastName+"\nGender: "+st.gender+"\nAge: "+st.age+"\nSports Event:"+st.sportsEvent); 
        st.elegibility(); 
      System.out.println("-----------------------------------------"); 
       } 
      } 
     } 
    } 
+0

你想達到什麼目的?添加新的輸入結束? – minigeek

+0

當我輸入第一個輸入值時,它會被添加到數組列表中。現在,當我第二次輸入時,第一個值會覆蓋顯示第二個輸入兩次。在哪裏不應該發生。 –

+0

請把它降低到[mcve]。這裏的大部分代碼與問題無關。此外,請花時間儘可能將代碼格式設置爲可讀。 –

回答

2

因爲你創建內環新的對象: 列表L =新的ArrayList();

嘗試這樣:

public static void main(String[] args) { 
    List<InputParticipant> stud=new ArrayList<InputParticipant>(); 
    //List<InputParticipant> l=new ArrayList<InputParticipant>(); 

    Scanner in = new Scanner(System.in); 
    while(true){ 
     System.out.println("Enter the Student details:"); 
     System.out.print("roll number:"); 
    rollNumber=in.nextInt(); 
    System.out.print("First name:"); 
    firstName=in.next(); 
    System.out.print("Last name:"); 
    lastName=in.next(); 
    System.out.print("age:"); 
    age=in.nextInt(); 
    System.out.print("gender:"); 
    gender=in.next(); 
    System.out.print("sports event(50meterrace/100meterrace/400meterrace/Juvelin/discus):"); 
    sportsEvent=in.next(); 
    stud.add(i); 
    InputParticipant i = new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent); 

    Iterator<InputParticipant> disp = l.iterator(); 
    while(disp.hasNext()){ 
     InputParticipant st=disp.next(); 
     System.out.println("Roll Number:"+st.rollNumber+"\nName: "+st.firstName+" "+st.lastName+"\nGender: "+st.gender+"\nAge: "+st.age+"\nSports Event:"+st.sportsEvent); 
     st.elegibility(); 
     System.out.println("-----------------------------------------"); 
    } 
    } 
    } 
0

可能嘗試使用Java POJO。

就你而言,在你從掃描儀獲得值後,創建一個新的對象,然後將該對象添加到你的ArrayList中。

i = new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent); 
stud.add(i); 

不確定爲什麼要在while循環中創建另一個ArrayList,因爲while只能用於收集輸入。

0

我不能真正幫你修復代碼,因爲很多人都是奇怪的關於它。

爲什麼它似乎你總是覆蓋有可能與使學生靜態,這意味着學生類的每個實例將具有相同的名稱,姓名,所有類成員做...

- 可能首先創建一個Student類,其中只包含帶有getter和setter的私有成員以及所有這些屬性的構造函數。

- 然後在一個單獨的Main類中創建主方法,這會阻止你做你不應該做的事情。

- 在該方法實例化一個列表後,AFTER啓動一個循環。

- 在這個循環你收集你1個學生的數據到本地變量(字符串名稱= in.readNext();絃樂的firstName = ....)

- 現在你實例化一個Student對象與構造函數,你只需將本地變量傳遞給它。

- 將新創建的Student對象添加到列表中。

- 當循環完成後,您應該有一個充滿學生的列表。

0

Student class和InputParticipant類的成員是靜態的。閱讀static members。 換句話說,ArrayList沒有被覆蓋,靜態成員就像是同一類中不同對象的共享變量。如果您將某些內容分配給一個對象中的靜態成員,則該成員將在所有對象中具有新值。只需刪除靜態關鍵字,並使其成爲實例成員。

public class Student { 
public int rollNumber, age; 
public String firstName, lastName, gender; 
public Student(int rollNumber, String firstName, String lastName, int age, String gender) 
{ 
    this.rollNumber = rollNumber; 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.age = age; 
    this.gender = gender; 
    } 
} 
+0

我意識到這一點。有什麼方法可以解決嗎? –

+0

答覆已更新。 –