简体   繁体   English

Android应用程序中的奇怪地图行为

[英]Strange map behavior in Android application

I have some strange problem with Map used in my application. 我在应用程序中使用Map遇到了一些奇怪的问题。

I have a list of objects - Table . 我有一个对象列表Table Each object has a field which a map of objects of another class: 每个对象都有一个字段,其中包含另一个类的对象的映射:

public class Table {

    private Map<Integer, MyObject> objectsMap = new HashMap<Integer, MyObject>();

    public void addToMap(int i, MyObject mOrder)
    {
        objectsMap.put(i, mOrder);
    }

    public MyObject getObject(int poz)
    {
        return objectsMap.get(poz);
    }

}

public class MyObject {

    private int info1;
    private int info2;

    public void setInfo(int i1, int i2)
    {
        this.info1 = i1;
        this.info2 = i2;
    }

    public int[] getInfos()
    {
        return new int[]{this.info1, this.info2};
    }

}

Then in my main Activity I have some initialization: 然后在我的主活动中,进行一些初始化:

private List<Table> tableList = new ArrayList<Table>();

//...

public void initTableObject(Table myTable)
{
    MyObject newObject = new MyObject();
    newObject.setInfo(1,2);
    myTable.addToMap(1, newObject);
    MyObject newObject = new MyObject();
    newObject.setInfo(3,4);
    myTable.addToMap(2, newObject);
    MyObject newObject = new MyObject();
    newObject.setInfo(5,6);
    myTable.addToMap(3, newObject);
    MyObject newObject = new MyObject();
    newObject.setInfo(7,8);
    myTable.addToMap(4, newObject);
}

The problem is when I want to check values: 问题是当我想检查值时:

for (Table table : tableList) {
    int[] info = table.getObject(4).getInfos();
    Log.d("TAG","4: {" + info[0] + ", " + info[1] + "}");
}

When I've got 1 Table object everything is ok. 当我有1个Table对象时,一切正常。 The problem begins when I have 2 or more objects. 当我有2个或更多对象时,问题就开始了。 In case with 2 elements after some interactions (not important in this case) I have problem. 如果在进行一些交互后有2个元素(在这种情况下不重要),我会遇到问题。

Each time, the last object from map of objects - that is newObject number 4 on the first Table object has the value equal to the 1st object value of 2 element. 每次,对象映射中的最后一个对象-第一个Table对象上的newObject number 4的值等于2个元素的第一个对象值。 The second Table object and its MyObject objects are ok. 第二个Table对象及其MyObject对象正常。

I was trying everything, and I don't see anything else so this is why I am asking here. 我正在尝试所有操作,但没有看到其他任何内容,因此这就是我在这里询问的原因。

Can someone tell me what is going on here that I am getting this error? 有人可以告诉我这里发生了什么错误吗?

I have been checking it, and it works perfectly for me. 我一直在检查它,它非常适合我。 I would say that the problem is not with the way Table is defined (the code shown), but in the way you're using it. 我会说问题不在于表的定义方式(显示的代码),而在于您使用它的方式。 You're probably modifying the instance of MyObject after you add it to the first Table. 将MyObject实例添加到第一个表后,您可能正在修改它。

I found an error and solution thanks to your comments. 感谢您的评论,我发现了错误和解决方案。

In fact I've been declaring myObject few times, but it was because if it wasn't like this values were wrong when added to the map in addToMap() method. 实际上,我已经声明了myObject几次,但这是因为,如果不这样,则在addToMap()方法addToMap()值添加到地图时,此值是错误的。 However, it came out, that this declaration was wrong and I needed to do it directly when I am adding it to the map in mentioned method, so now it looks like this: 但是,结果表明,该声明是错误的,当我将其添加到上述方法中的地图时,我需要直接进行声明,因此现在看起来像这样:

public void addToMap(int i, MyObject mOrder)
{
   objectsMap.put(i, new MyObject(mOrder));
}

Also I needed to add proper constructor in MyObject class. 另外,我需要在MyObject类中添加适当的构造函数。 And now it is working great. 现在,它运行良好。

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

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