无论如何,HashMap都返回Null [英] HashMap returns Null no matter what

查看:111
本文介绍了无论如何,HashMap都返回Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在概念化这个问题时遇到了一些麻烦,所以我提前道歉,如果它是模糊的或者在其他地方解决了。



无论如何,我有一个大称为 Board的类。板上是 ArrayList< Space> sList ,板上的空间列表。每个空间是 bList ,这是一个表示哪些空间与这个特定空间相交的HashMap。 Board init()方法创建所有的空格并给它们的边界,例如

  sList.add(new Location(7,true,Police Station)); 
sList.get(3).addToBList(sList.get(7)); //在sList索引中增加Police Station给bList index 3

然而,如果在 Board ,我试着看看某个空间的 bList ,它总是返回null,就像我从未向bList添加过任何东西。但是,如果我在Space中查看,bList总是充满了项目。所以,基本上,我试图从另一个类的类中调用一个变量,并且它只显示非空值(如果我在其他类中) - 任何想法(或者澄清问题)?



编辑:我想我要澄清我自己的问题。



如果Space有一个getBList()getter返回bList这是一个哈希映射),并在委员会中,我添加了某些值,该bList,为什么bList返回{},当我打电话给董事会的getBList()?$ / b>
$ b

,这是所有的箭头(见底部)。



代码本身:

  public class Board {

ArrayList< Space> SLIST;
ArrayList< Player> plist中;
int moveCount = 0;
ArrayList< Space> visited = new ArrayList< Space>();
$ b $ public Board(int p,int oldGod){//玩家数量,Old God
sList = new ArrayList< Space>();
pList = new ArrayList< Player>();
init();

move(sList.get(0),sList.get(34));



公共空间移动(空间开始,空间结束){
visited.add(start);
Iterator it = start.bList.entrySet()。iterator(); //这是麻烦开始的地方。 bList只返回{}
if(start.equals(end)){
System.out.println(Here!You've been at+ start);
}
else {System.out.println(调试时间,你在+ start); (it.hasNext()){
Map.Entry pair =(Map.Entry)it.next();
for(Space s:visited){
if(pair.getValue()。equals(s)){
}
else {

return移动((空间)pair.getValue(),结束);
}

}
}
}
return start;


private void init(){
sList.add(new Location(1,true,Ma's Boarding House));
sList.add(new Location(2,true,South Church));
sList.get(1).addToBList(sList.get(2));
//等等...
}
}


公共抽象类空间{
int district;
字符串名称;
HashMap<箭头,空格> bList = new HashMap< Arrow,Space>();
ArrayList< Creep> cList = new ArrayList< Creep>();
ArrayList< Player> pList = new ArrayList< Player>();

public String toString(){
return name;
}

public void addToBList(Space s,Arrow a){
bList.put(a,s);

$ b $ public void borderList(){//在这里它总是将bList看作是变量的全部
Iterator it = bList.entrySet()。iterator(); (it.hasNext()){
Map.Entry pair =(Map.Entry)it.next();
System.out.println(this +:+ pair.getKey()+ - >+ pair.getValue()); //箭头指向
it.remove();
}
}



}


公共课程箭头{

int color;

public Arrow(int c){
color = c;


public String toString(){
if(color == 0)
returnBlack;
else if(color == 1)
returnWhite;
else if(color == 2)
returnBoth;
else
返回None;
}

}


解决方案

我建议您阅读Javadoc以获取 Object.hashCode()


返回对象的哈希码值。这种方法支持散列表(如HashMap提供的散列表)。

docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-rel =nofollow> Object.equals()



除非你重写了这些,否则两个对象是不一样的,除非它们是相同的对象,不管你在它们中设置了什么值。



对于键,您必须实现这两种方法。对于地图的值,如果你不使用它,你可能只实现 equals(Object)



顺便说一句:我建议你阅读 java.lang java.util

I'm having a little trouble conceptualizing this question, so I apologize in advance if it's vague or been solved elsewhere.

Anyway, I've got a big class called Board. In board is ArrayList<Space> sList, a list of Space on the Board. Which each Space is bList, a HashMap that represents which Spaces border this particular Space. The init() method of Board creates all the Spaces and gives them their borders, for example

sList.add(new Location(7, true, "Police Station"));
sList.get(3).addToBList(sList.get(7)); //adds "Police Station" to the bList at sList index 3

However, if, while in Board, I try and look the bList of a certain Space, it always returns null, like I never added anything to bList. However, if I look within the Space itself, bList is always full of items. So, basically, I'm trying to invoke a variable from a class in another class and it only shows non-null values if I'm within that other class itself - any ideas (or clarifying questions)?

EDIT: I think I'm going to clarify my own question.

If Space has a getBList() getter that returns bList (which is a hashMap), and in Board I add certain values to that bList, why does bList return "{}" when I call getBList() in Board?

Also, this is all Arrow is (see bottom).

The code itself:

public class Board {

ArrayList<Space> sList;
    ArrayList<Player> pList;
    int moveCount = 0;
    ArrayList<Space> visited = new ArrayList<Space>();

    public Board(int p, int oldGod){ //num of players, Old God
        sList=new ArrayList<Space>();
        pList=new ArrayList<Player>();
        init();

        move(sList.get(0), sList.get(34));

}

public Space move(Space start, Space end){ 
        visited.add(start);
        Iterator it = start.bList.entrySet().iterator();//this is where the trouble starts. bList returns only {}
        if(start.equals(end)){
            System.out.println("Here! You've arrived at " + start);
        }
        else{System.out.println("Time to debug. You're at " + start);
        while(it.hasNext()){
            Map.Entry pair = (Map.Entry)it.next();
            for(Space s : visited){
                if(pair.getValue().equals(s)){
                }
                else{

                    return move((Space)pair.getValue(), end);
                }

            }
        }
        }
        return start;
    }

    private void init() {
        sList.add(new Location(1, true, "Ma's Boarding House"));
        sList.add(new Location(2, true, "South Church"));
        sList.get(1).addToBList(sList.get(2));
        //etc...
}
}


public abstract class Space {
    int district;
    String name;
    HashMap<Arrow, Space> bList = new HashMap<Arrow, Space>();
    ArrayList<Creep> cList = new ArrayList<Creep>();  
    ArrayList<Player> pList = new ArrayList<Player>();

    public String toString() {
        return name;
        }

    public void addToBList(Space s, Arrow a){
        bList.put(a, s);
    }

    public void borderList(){ //in here it always sees bList as full of variables
        Iterator it = bList.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry pair = (Map.Entry)it.next();
            System.out.println(this + ": " + pair.getKey() + "-> " + pair.getValue()); //arrow towards
            it.remove();
        }
    }



}


public class Arrow {

    int color;

    public Arrow(int c){
        color=c;
    }

    public String toString(){
        if(color==0)
            return "Black";
        else if(color==1)
            return "White";
        else if(color==2)
            return "Both";
        else
            return "None";
    }

}

解决方案

I suggest you read the Javadoc for Object.hashCode()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

and Object.equals()

Unless you override these, two object are not the same, unless they are the same object, regardless of what values you set in them.

For keys, you must implement both methods. For values of a map you might only implement equals(Object) or not if you don't use it.

BTW: I suggest you read the Javadoc for all the classes in java.lang and java.util

这篇关于无论如何,HashMap都返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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