关于Hashmap的问题

时间:2017-05-16 10:19:56

标签: java

这是我的代码,这是我第一次使用HashMap。 我真的不明白为什么key" a"在为键" d"分配值后更改。似乎关键" a"的价值。每次为anther键赋值时都会重写。

import java.util.HashMap;
import java.io.*;

class BowlingSystem {

    static HashMap<String, BowlingSystem> bs=new HashMap();//abbreviate for Bowling System
    public String Name;
    public int no_game;//same as above
    public String[] date;
    public int total_score=0;
    public String last_date;
    public int last_score;
    public int[] score;

    public BowlingSystem(String Name_i, String[] date_i, int[] score_i){
        score=score_i;
        Name=Name_i;
        date=date_i;
    }

    public void register (BowlingSystem information){
        bs.put(information.Name,information);
    }

    public void out(String input){
        if (bs.containsKey(input)) {
            BowlingSystem information=(BowlingSystem)bs.get(input);
            no_game=information.date.length-1;
            last_date=information.date[no_game];
            last_score=information.score[no_game];

            for(int i=1;i<=no_game;i++){
                total_score=total_score+score[i];
            }

            double avg=(double)total_score/no_game;
            System.out.println("Name is "+information.Name+"\n");
            System.out.println("Number of games is "+no_game+"\n");
            System.out.println("Average score of all of the games is "+avg+"\n");
            System.out.println("The score of the last game is "+last_score+"\n");
            System.out.println("The date of the last game is "+information.date[no_game]+"\n");
        }
    }
}

    public class Homework06 {
        public static void main(String [] args) throws IOException{
        int[] scores=new int[5];
        String[] dates=new String[5];

for (int i=1;i<=4;i++){//test data
            scores[i]=20+i;
            dates[i]="01/"+i+"/2017";
        }

        BowlingSystem a01=new BowlingSystem("a",dates,scores);
        a01.register(a01);

        for (int i=1;i<=4;i++){//test data
            scores[i]=30+i;
            dates[i]="02/"+i+"/2017";
        }

        BowlingSystem b01=new BowlingSystem("b",dates,scores);
        b01.register(b01);

        for (int i=1;i<=4;i++){//test data
            scores[i]=40+i;
            dates[i]="03/"+i+"/2017";
        }
        BowlingSystem c01=new BowlingSystem("c",dates,scores);
        c01.register(c01);

        for (int i=1;i<=4;i++){//test data
            scores[i]=20+i;
            dates[i]="05/"+i+"/2017";
        }

        BowlingSystem d01=new BowlingSystem("d",dates,scores);
        d01.register(d01);
        a01.out("a");//the outcome of this now is same as key "d"
        b01.out("b");//the outcome of this now is same as key "d"
        System.out.println("Please type in the name, in this case name is just a letter in lower case from a to d.\n");//below this line is not important
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in), 1);
        String name=stdin.readLine(); 
    }
}

2 个答案:

答案 0 :(得分:1)

所有BowlingSystem个实例(Map的值)都引用了传递给其构造函数(datesscores)的相同数组,因此修改了内容数组会影响所有数组。

为了避免这种情况,您的BowlingSystem应该复制传递给它的数组,或者您应该将不同的数组传递给您正在创建的每个实例。

例如,

替换

BowlingSystem a01=new BowlingSystem("a",dates,scores);

BowlingSystem a01=new BowlingSystem("a",new String[5],new int[5]);

并为每个构造函数调用执行相同的操作。

答案 1 :(得分:0)

这是因为datesscores对于HashMap中的所有键都是相同的。这就是为什么当您打印数据时,会显示最后添加到datesscores的数据。