为什么我一直只获得 Java ArrayList 中的最后一个对象值? [英] Why do I keep getting only tha last object value in Java ArrayList?

查看:22
本文介绍了为什么我一直只获得 Java ArrayList 中的最后一个对象值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已设法将所有对象放入数组列表中,但无法打印所有值.无论使用何种方法,都只打印最后一个.

I have managed to put all objects into arraylist but I am not able to print all values. Only the last one is getting printed, regardless of method used.

它不仅仅通过 ArrayList 打印,这让我怀疑推送的对象是否相同.

It is not getting printed through ArrayList only, which makes me wonder if the objects pushed are the same. 

如果是,我该如何更改?我附上了程序(运行 FileInput.java):

If it is, how do I change that? I have attached the program (run 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";
        }       
*/      
}

推荐答案

简单:

 Player1 user = new Player1();

您一次又一次地添加相同对象.将该语句放入您的循环中.您希望在每次循环迭代期间完全添加一个新的播放器对象!

You are adding the same object again and again. Put that statement into your loop instead. You want to add a completely new Playwer object during each loop iteration!

但即便如此,事情也不会解决;因为(正如 Eran 认为的):您的 Player 类只有 static 字段.那就像作弊";因为这意味着所有 Player 对象也会看到相同字段(因为静态字段在类的所有实例之间共享!)

But even then, things wouldn't work out; because (as Eran figured): your Player class has only static fields. That is like "cheating"; because it means that all Player objects would see the same fields, too (because static fields are shared between all instances of a class!)

换句话说:静态是好的 OO 设计中的一个异常.你不使用它作为默认值;相反:您只在特殊的极端情况下将字段设为静态(请参阅此处以获得一些示例).

In other words: static is an abnormality in good OO design. You don't use it as default; to the contrary: you only make fields static in special corner cases (see here for some examples).

这篇关于为什么我一直只获得 Java ArrayList 中的最后一个对象值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆