【问题标题】:Check length of an array and check if HashMap key is in the HashMap (Java, Arrays, HashMaps)检查数组的长度并检查 HashMap 键是否在 HashMap 中(Java、Arrays、HashMaps)
【发布时间】:2016-01-15 19:40:54
【问题描述】:

我正在尝试做一些错误捕获。

错误应该检查数组的长度是否小于2,并检查HashMap是否包含用户输入的键。

捕获的错误必须仅使用 if 语句,并且必须使用 .length() 方法,并且必须使用 HashMap java API 中的 .get() 方法。

我已经像这样声明了 HashMap:

 private HashMap <String, Shape> shapes;

下面是我的其余代码。

        String basicCommand = commands[0];
        String moreCommands = commands[1];

    int size = commands.length;

    if(size < 2 && shapes.get(commands[1]).equals(commands[0])){

        if(basicCommand.equals("circle")) {
            makeACircle(commands);
        }
        else if(basicCommand.equals("help")) {
            printHelp();
        }
        else if(moreCommands.equals("left")){
            moveLeft(commands);
        }
        else if(moreCommands.equals("down")){
            shapes.get(commands[0]).moveDown();
        }
        else if(moreCommands.equals("up")){
            shapes.get(commands[0]).moveUp();
        }
        else if(moreCommands.equals("right")){
            shapes.get(commands[0]).moveRight();
        }
        else if(moreCommands.equals("visible")){
            shapes.get(commands[0]).makeVisible();
        }
        else if(moreCommands.equals("invisible")){
            shapes.get(commands[0]).makeInvisible();
        }
        else if(commands[0].equals("forget")){
            shapes.remove(commands[1]).makeInvisible();
        }
        else {
            System.out.println("Unknown command: " + basicCommand);
        }
    }
    else{
        System.out.println("error");
    }
}

【问题讨论】:

    标签: java arrays error-handling hashmap logic


    【解决方案1】:

    通过方法检查map是否包含key

    shapes.containsKey(yourKey);
    

    以及地图中元素的大小(如果这是您通过该方法查找的内容):

    shapes.entrySet().size();
    

    希望对你有帮助。

    【讨论】:

    • 感谢卢西亚诺阿尔梅达。我已经根据您在下面的回答发布了我的解决方案。它已经解决了这个问题!再次感谢。
    【解决方案2】:

    感谢卢西亚诺·阿尔梅达。你是对的!

    这就是我根据您的回答解决问题的方法。

    如果你们中的任何人对下面的代码有任何改进或建议,请发表评论。谢谢!

    private void execute(String[] commands) {

        String basicCommand = commands[0];
        String moreCommands = commands[1];
    
        int size = commands.length;
    
        if(basicCommand.equals("circle")) {
            makeACircle(commands);
        }
        else if(basicCommand.equals("help")) {
            printHelp();
        }
        else if(shapes.containsKey(commands[0])){
            if(moreCommands.equals("left")){
                shapes.get(commands[0]).moveLeft();
            }
            else if(moreCommands.equals("down")){
                shapes.get(commands[0]).moveDown();
            }
            else if(moreCommands.equals("up")){
                shapes.get(commands[0]).moveUp();
            }
            else if(moreCommands.equals("right")){
                shapes.get(commands[0]).moveRight();
            }
            else if(moreCommands.equals("visible")){
                shapes.get(commands[0]).makeVisible();
            }
            else if(moreCommands.equals("invisible")){
                shapes.get(commands[0]).makeInvisible();
            }
            else{
                System.out.println("error");
            }
        }        
        else if(commands[0].equals("forget")){
            shapes.get(commands[1]).makeInvisible();
            shapes.remove(commands[1]);
        }
        else {
            System.out.println("Unknown command: " + basicCommand);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 2015-12-31
      • 2021-07-16
      • 1970-01-01
      • 2021-10-09
      相关资源
      最近更新 更多