检查数组的长度并检查 HashMap 键是否在 HashMap (Java, Arrays, HashMaps) 中

Check length of an array and check if HashMap key is in the HashMap (Java, Arrays, HashMaps)

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

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

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

我已经这样声明了 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");
    }
}

通过

方法检查映射是否包含键
shapes.containsKey(yourKey);

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

shapes.entrySet().size();

希望对您有所帮助。

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

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

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

private void execute(字符串[]命令) {

    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);
    }

}