2017-04-18 69 views
-1

我已經設法將所有對象都放入了數組列表中,但我無法打印所有值。無論使用什麼方法,只有最後一個打印。爲什麼我一直只獲取Java ArrayList中的最後一個對象值?

它沒有通過ArrayList打印,這讓我想知道推送的對象是否相同。

如果是這樣,我該如何改變它?我重視程序(運行FileInput.java):

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.StringTokenizer; 

import javax.swing.text.html.HTMLDocument.Iterator; 

//lecture notes(L1) method 
public class FileInput { 
    static String first; 
    static String second; 
    static String third; 
    static String fourth; 
    static String fifth; 
    static String sixth; 

    static int num = 1; 
     public static void main(String[] args)throws FileNotFoundException, IOException{ 
     Scanner input = new Scanner(new File("player.txt")); 
     String data = null; 

     PrintWriter output = new PrintWriter("outfile.txt"); 

     // Player1 user = new Player1(); 

     ArrayList<Player1>listOfPlayers = new ArrayList<>(); 

     Player1 user = new Player1(); 

     // Tokenizing 
     System.out.println("CSCI213 Players Management System"); 
     while(input.hasNextLine()){ 

      System.out.println("\nPlayer " + num); 
      data = input.nextLine(); 
      StringTokenizer token = new StringTokenizer(data,"|"); 

      // int t = token.countTokens(); 
      // System.out.println("t is:" + t); 

      first = token.nextToken().trim(); 
      user.setLoginname(first); 

      second = new String(token.nextToken("|")); 
      user.setPassword(second); 

      third = new String(token.nextToken("|")); 
      user.setChips(third); 

      fourth = new String(token.nextToken("|")); 
      user.setUsername(fourth); 

      fifth = new String(token.nextToken("|")); 
      user.setEmail(fifth); 

      sixth = new String(token.nextToken("|")); 
      user.setBirthdate(sixth); 

      // user.display(); 

      listOfPlayers.add(user); 
      System.out.println("Size is: " + listOfPlayers.size()); 
      // System.out.println(user.loginname); 
      // System.out.println(listOfPlayers.get(num-1).loginname); 
      num++; 
      // output.write(data); 
      // output.write("\r\n"); 
     } 
     int x = listOfPlayers.size(); 
     System.out.println("Size is: " + x); 
     System.out.println(listOfPlayers); 

     // Display address of required 
     Player1 p = new Player1(); 

     for(int i = 0; i < x; i++){ 
      p = listOfPlayers.get(i); 
      System.out.println(p.loginname); 
     } 

     for(Player1 e:listOfPlayers){ 
      System.out.println(e.loginname); 
      System.out.println(e.email); 
     } 

     while(input.hasNextLine()){ 
      data = input.nextLine(); 
      output.write(data); 
      output.write("\r\n"); 
     } 

     input.close(); 
     output.close(); 
    } 
} 

// Store all player information 
public class Player1 { 
     static String loginname; 
     static String password; 
     static String chips; 
     static String username; 
     static String email; 
     static String birthdate; 

     /* 
     public Player1(String loginname, String password, 
       String username, String email, String birthdate){ 
      this.loginname = loginname; 
      this.password = password; 
      this.username = username; 
      this.email = email; 
      this.birthdate = birthdate; 
     } 
     */ 

     public Player1() { 
      // TODO Auto-generated constructor stub 
     } 

     public static String getLoginname() { 
      System.out.print("loginname: "); 
      return loginname; 
     } 

     public static void setLoginname(String loginname) { 
      Player1.loginname = loginname; 
     } 

     public static String getPassword() { 
      System.out.print("password: "); 
      return password; 
     } 

     public static void setPassword(String password) { 
      Player1.password = password; 
     } 

     public static String getUsername() { 
      System.out.print("username: "); 
      return username; 
     } 

     public static void setUsername(String username) { 
      Player1.username = username; 
     } 

     public static String getEmail() { 
      System.out.print("email: "); 
      return email; 
     } 

     public static void setEmail(String email) { 
      Player1.email = email; 
     } 

     public static String getBirthdate() { 
      System.out.print("birthdate: "); 
      return birthdate; 
     } 

     public static void setBirthdate(String birthdate) { 
      Player1.birthdate = birthdate; 
     } 

     public static String getChips() { 
      System.out.print("chips: "); 
      return chips; 
     } 

     public static void setChips(String chips) { 
      Player1.chips = chips; 
     } 

     public void display() { 
      System.out.println("Name: " + this.username); 
      System.out.println("Email: " + this.email); 
      System.out.println("Birthdate: " + this.birthdate); 
      System.out.println("Login ID: " + this.loginname); 
      System.out.println("Balance Chips: " + this.chips); 
     } 
/* 
     @Override 
     public String toString() { 
      return "toString()=" + this.loginname + "\n"; 
     }  
*/  
} 
+0

you're八方通工作Player'的'相同的實例,就好像只會有 「一」 成爲現實比如在你的'列表',但這幾次。 – SomeJavaGuy

+0

你只創建一個'Player1'的實例,在你的循環中,你只是更新對象的同一個實例,然後將它重複添加到'List'中 – MadProgrammer

回答

4

簡單:

Player1 user = new Player1(); 

你一次又一次地加入相同對象。將該語句代入而不是您的循環。你想在每個循環迭代期間添加一個完全新的Playwer對象!

但即使如此,事情不會奏效;因爲(正如Eran所說):你的Player類只有靜態字段。這就像「作弊」;因爲這意味着所有的球員對象會看到相同領域,太(因爲靜態字段共享一個類的所有實例之間!)

換句話說:靜態處於良好的異常OO設計。你不使用它作爲默認值;相反:您只能在特殊的角落情況下將字段設置爲靜態(有關示例,請參見here)。

2

你有兩個錯誤:

  1. 你一遍又一遍地將相同Player1實例的列表。您應該將Player1 user = new Player1();移動到添加球員的循環中。

    變化

    Player1 user = new Player1(); 
    
    // Tokenizing 
    System.out.println("CSCI213 Players Management System"); 
    while (input.hasNextLine()) { 
    

    // Tokenizing 
    System.out.println("CSCI213 Players Management System"); 
    while (input.hasNextLine()) { 
        Player1 user = new Player1(); 
    
  2. Player1類的成員都是靜態的,所以即使你解決了第一個問題,中Player1所有實例將共享這些成員。您應該將它們更改爲非靜態。

    變化

    public class Player1 { 
        static String loginname; 
        static String password; 
        static String chips; 
        static String username; 
        static String email; 
        static String birthdate; 
    

    public class Player1 { 
        String loginname; 
        String password; 
        String chips; 
        String username; 
        String email; 
        String birthdate; 
    
+3

忽略了......很高興你向下滾動到結束 ;-) – GhostCat

相關問題