简体   繁体   English

从另一个类访问HashMap数据时出现问题

[英]Problem accessing HashMap data from another class

Am having a problem accessing the data in a HashMap. 在访问HashMap中的数据时遇到问题。 It was created in one class and is being called from another. 它是在一个类中创建的,并且是从另一个类中调用的。 See below; 见下文;

Created 创建

public class LoadDatabase {
    public Map virusDatabase = new HashMap();
    ...
    public void toHash(String v_Name, String signature) {
        virusDatabase.put(v_Name, signature);
    }
    ...
    public void printDatabase() {   // This method is displaying correct data, so is being stored.
        Iterator iterator = virusDatabase.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            System.out.println(key + " = " + virusDatabase.get(key));
        }
    }
    ...
}

Need Access 需要访问

public class LCS {
    LoadDatabase lb = new LoadDatabase();
    Tokenizer T = new Tokenizer();
    ...
    public void buildDataLCS(String[] inTokens) {
        Iterator iterator = lb.virusDatabase.keySet().iterator();
        ...                
        while (iterator.hasNext()){
            String key = (String) iterator.next();
            String v_sig = (String) lb.virusDatabase.get(key);
            System.out.println(v_sig);  //Example of problem, nothing printed
        ...
    }
    ...
}

Why is the problem happening? 为什么会出现问题? Could you point me in the right direction. 你能指出我正确的方向吗?

Either of the 2 issues, 两个问题中的任何一个,

  1. You are not putting anything there. 你没有放任何东西。 As I can't see your invocation of toHash(String v_Name, String signature) method. 因为我看不到你调用toHash(String v_Name, String signature)方法。

  2. You are using 2 different instances of LoadDatabase class, somehow. 您正在以某种方式使用2个不同的LoadDatabase类实例。 Try making LoadDatabase singleton. 尝试制作LoadDatabase单例。

Carlos 卡洛斯

I suspect you are not putting what you think you are putting into the map, or the keys when you put data in are not the same as when you take values out. 我怀疑你没有把你认为放在地图中的东西放在地图上,或者你输入数据时的键与你取出值时的键不一样。 I would log/print the key/val you put in, and then log/print the key/val you try to get out. 我会记录/打印你输入的键/值,然后记录/打印你试图离开的键/值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM