【问题标题】:Hashmap not replacing the correct itemsHashmap 没有替换正确的项目
【发布时间】:2015-05-18 14:12:57
【问题描述】:

在 main 中运行方法时,我试图用新值替换 Hotel_A。但是为键插入了错误的值。

例如,当您将 Hotel_A 更改为采用 test1 时,Hotel_B 将采用该值。我已经调试过了,if语句的逻辑是正确的。我认为 fetchImage 方法不正确,正在寻找错误的酒店。

预期输出:

尺寸:2

Hotel_A:[B@3dfeca64

酒店_B:[B@1aa8c488

收到的输出:

尺寸:2

Hotel_A:[B@1aa8c488

Hotel_B:[B@3dfeca64

public class Main{

    public static void main(String[] args){
        ImageStore i = new ImageStore();
        byte[] test1 = { (byte) 0x0b, (byte) 0x1f };
        byte[] test2 = { (byte) 0x0a, (byte) 0x1d };

        //Functionality test part 1
        System.out.println("Functionality test part 1");
        i.storeImage("Hotel_A",test2);
        i.storeImage("Hotel a",test2);
        i.storeImage("Hotel B",test1);

        //"The store should have size 2"
        System.out.println("Size: " + i.size());

        //"And contain imageA (test1) and imageB (test2)"
        System.out.println("Hotel_A: " + i.fetchImage("Hotel_A"));
        System.out.println("Hotel_B: " + i.fetchImage("Hotel_B"));

        i.imageObj.clear();


        //Functionality test part 2
        System.out.println("\nFunctionality test part 2");
        i.storeImage("Hotel_A", test1);
        i.storeImage("Hotel a", test1);
        i.storeImage("Hotel_A", test2); //Example included syntax error, important?
        i.storeImage("Hotel a",test2);

        //"The store should have size 1"
        System.out.println("Size:" + i.size());

        //"and only contain imageB"
        System.out.println("Hotel_A: " + i.fetchImage("Hotel_A"));

    }
}



import java.util.Map;
import java.util.HashMap;

public class ImageStore {

    //Declaration of HasMap for id/ byte array storage
    Map<String, byte[]> imageObj = new HashMap<String, byte[]>();

  /**
   * Inserts an image in the store
   *
   * @param id -- The identifier of the image
   * @param content -- The content of the image
   */
  public void storeImage (String id, byte[] image) {
      //Check's input 
      //If Equal to 'Hotel_A' or similar, saves to 'Hotel_A' index
      if (id.equals("Hotel_A") || id.equals("Hotel_a") || 
              id.equals("Hotel A") || id.equals("Hotel a"))
      {
          imageObj.put("Hotel_A",image);
      }

      //Else if equal to 'Hotel_B' or similar, saves to 'Hotel_B' index
      else if (id.equals("Hotel_B") || id.equals("Hotel_b")
              || id.equals("Hotel B") || id.equals("Hotel b"))
      {
          imageObj.put("Hotel_B",image);
      }
  }


  /**
   * Retrieves an image from the store
   *
   * @param id -- The identifier of the image to be retrieved
   * @return the image content
   */   
  public byte[] fetchImage(String id) {
      return imageObj.get(id);
  }

  /**
   * The size of the store
   *
   * @return the actual store size
   */
  public int size() {
    return imageObj.size();
  }
}

【问题讨论】:

  • 那么,在运行这段代码时,您期望会发生什么,会发生什么?
  • 我希望输入 Hotel_A 的值首先打印,然后是 Hotel_B 的值第二个打印。它似乎可以工作,但是如果您将 Hotel_A 更改为接受 test2,新值将出现在第二个而不是第一个,这意味着它在应该打印 Hotel_A 时打印的是 Hotel_B,或者它们被错误地分配
  • 请准确。编辑您的问题并粘贴您从程序中获得的输出。然后打印你期望得到的输出,并解释原因。
  • 好的,我已经完成了
  • 我强烈建议更改为测试程序以产生更有意义的输出。目前,您正在获取一个数组的哈希码,它是运行和系统相关的。例如,Arrays.toString( i.fetchImage("Hotel_A"))

标签: java reference hashmap compare


【解决方案1】:

将 toString() 添加到 fetchImage 表明我拥有正确的值。我的错。谢谢Particia Shanahan

【讨论】:

    猜你喜欢
    • 2015-01-11
    • 2015-10-31
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    相关资源
    最近更新 更多