我已經設法將所有對象都放入了數組列表中,但我無法打印所有值。無論使用什麼方法,只有最後一個打印。爲什麼我一直只獲取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";
}
*/
}
you're八方通工作Player'的'相同的實例,就好像只會有 「一」 成爲現實比如在你的'列表',但這幾次。 – SomeJavaGuy
你只創建一個'Player1'的實例,在你的循環中,你只是更新對象的同一個實例,然後將它重複添加到'List'中 – MadProgrammer