【问题标题】:Why does using a for loop change the output for this dictionary class? [duplicate]为什么使用 for 循环会更改此字典类的输出? [复制]
【发布时间】:2019-02-28 05:07:21
【问题描述】:

我实际上是在创建自己的 Dictionary 类,它作为堆栈工作。我可以将键值对(我自己创建的另一个类)放入我的字典中,该字典将它们保存在一个数组中。当我通过逐行纠正对 put() 的每个调用来手动放置键值对时,它工作正常。但是当我使用 for 循环时,它不能正常工作。如果我检查一个键是否在我的字典中,它会在使用 for 循环创建的字典中返回 false,而在另一个字典中返回 true。两个字典都使用我创建的方法打印完全相同的内容。但是使用 for 循环创建的字典在使用方法时不会给出正确的输出。

这是主要的代码:

有人知道为什么这两个字典不同吗?

编辑:我的 put() 代码如下

public class Dictionary implements Map<String, Integer> {

    private final static int INITIAL_CAPACITY = 10;
    private final static int INCREMENT = 5;
    private int count;

    Pair[] elems;

    public int getCount() {
      return count;
    }

    public int getCapacity() {
      return elems.length;
    }

    public Dictionary() {
        elems = new Pair[INITIAL_CAPACITY];
        count = 0;
    }

    @Override
    public void put(String key, Integer value) {
        Pair elem = new Pair(key, value);
        elems[count] = elem;
        count++;

        if (count == elems.length) {
          increaseCapacity();
        }

    }

    private void increaseCapacity() {
      Pair[] newElems = new Pair[count + INCREMENT];
      for (int i = 0; i < count; i++) {
        newElems[i] = elems[i];
      }
      elems = newElems;
    }

    @Override
    public boolean contains(String key) {
        for (int i = 0; i < count; i++) {
          if (elems[i].getKey() == key) {
            return true;
          }
        }
        return false;
    }

    @Override
    public Integer get(String key) {
      boolean got = false;
      Integer value = 0;

        for (int i = count - 1; i > - 1; i--) {
          if (elems[i].getKey() == key && !got) {
            value = elems[i].getValue();
            got = true;
          }
        }

        return value;

    }

    @Override
    public void replace(String key, Integer value) {
      Pair newPair;
      for (int i = count - 1; i > - 1; i--) {
        if (elems[i].getKey() == key) {
          newPair = new Pair(key, value);
          elems[i] = newPair;
        }
      }
    }

    @Override
    public Integer remove(String key) {
      Integer saved = null;
      boolean removed = false;
      for (int i = count - 1; i > - 1; i--) {
        if (elems[i].getKey() == key && !removed) {
          removed = true;
          saved = elems[i].getValue();
          elems[i] = null;
        }
      }

      return saved;
    }

    @Override
    public String toString() {
      String res;
      res = "Dictionary: {elems = [";
      for (int i = count-1; i >= 0 ; i--) {
          res += elems[i];
          if (i > 0) {
              res += ", ";
          }
      }
      return res +"]}";
    }

}

Pair 是我创建的另一个类,它只表示一个键值对。

【问题讨论】:

  • 为什么不打印这些值,看看它们是什么?
  • 我刚刚使用 Hashmap 制作了您的示例,它可以正常工作,因此问题存在于您的字典实现中。请把这个类的代码
  • 大胆猜测:您的Dictionary 类使用== 而不是equals() 比较字符串
  • @RaV1oLLi 把你所有的字典类代码放不只放方法

标签: java arrays for-loop


【解决方案1】:

好的,我只是重复你的问题。

字典:

public class Dictionary {
    Map<String, Integer> map = new HashMap<>();

    public boolean contains(String key) {
        for(String mapKey : map.keySet()) {
            if(mapKey == key) {
                return true;
            }
        }
        return false;
    }
}

主要:

public static void main(String[] args) {
    Dictionary dict = new Dictionary();

    dict.map.put("X" + 0, 0);
    dict.map.put("X" + 1, 1);
    dict.map.put("X" + 2, 2);
    dict.map.put("X" + 3, 3);
    System.out.println(dict.contains("X2")); // true

    Dictionary dict2 = new Dictionary();
    for(int i=0; i<4; i++) {
        dict2.map.put("X" + i, i);
    }
    System.out.println(dict2.contains("X2")); // false
}

存在问题是因为我使用 == 而不是 equals 方法比较字符串值,所以我几乎可以肯定你会犯同样的错误。您必须使用 equals 方法来比较字符串,否则您可能会遇到这种奇怪的情况

【讨论】:

  • 我将其全部更改为 .equals() 并且有效
  • 你实现的这个地图是来自 java.util.Map 吗?因为在您的情况下,“put”方法应该返回 Integer 而不是 void
  • 不,这些都是我自己创建的类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
相关资源
最近更新 更多