简体   繁体   English

Java-不从HashMap中删除

[英]Java- not removing from HashMap

OK so I have this HashMap 好,所以我有这个HashMap

private Map<String, Player> players = new HashMap<String, Player>();

Here is what I use to remove: 这是我用来删除的内容:

      public void destroy() {

players.remove("Red");
os.println(me.getUsername() + "|1|has left|yes|chat");

      }

I say Red because it's just a TEST right now. 我说红色是因为它现在只是一个测试。 I will get the eventual correct one later. 稍后我会得到正确的答案。 Anyways... 无论如何...

I use THIS to check. 我用这个来检查。

  if (e.getKeyCode() == KeyEvent.VK_Q) {
            for (Player playert : players.values()) {
                                        c.append("\n < "+playert.getUsername() + " > ");
                        }
                    }

When I'm all by myself.. I press Q and I get: 当我一个人呆着的时候..我按Q,我得到:

< Dan >

then my friend Red logs in and I Press Q... I get: 然后我的朋友Red登录,然后按Q ...,我得到:

< Dan >
< Red >

then he leaves I press Q and I get: 然后他离开我按Q,我得到:

< Dan >
< Red >

So.. how come this isn't working? 所以..这怎么不起作用?

Also, here is the code that gets called in init() when a player logs in the game (starts the applet) 另外,这是当玩家登录游戏(启动小程序)时在init()中调用的代码。

 public void playerLogin() throws IOException {

            Random roll = new Random();
            int newNo = roll.nextInt(200);
            // me.getUsername() = "Guest #" + roll.nextInt(110);
            // String me.getUsername() = getParameter("name");

            me = new Player();
            me.setUsername(getParameter("name"));
            me.setPlayerImage(ImageIO.read(getClass().getResource("me.gif")));
            me.setX(256);
            me.setY(256);
            me.setMap(1);
            me.setCommand("move");
            players.put(me.getUsername(), me);

            repaint();

            System.out.println(me.getUsername() + " was added. player: " + me);
            os.println(me.getUsername() + "|" + me.getX() + "|" + me.getY() + "|"
                        + me.getMap() + "|" + me.getCommand());

            attack = 4;
            defense = 5;
            gold = 542;
            level = 1;
            exp = 53;

      }

In other words, your Applet#destroy() method is not called at the moment you expect it is called? 换句话说,您的Applet#destroy()方法在您期望被调用的那一刻没有被调用? You should use Applet#stop() . 您应该使用Applet#stop() The destroy() is only called when the object in question is eligible for GC and/or when the whole browser instance is been closed (and thus not only the current page/tab). 仅在有问题的对象符合GC条件时和/或在关闭整个浏览器实例(因此不仅是当前页面/选项卡)时,才调用destroy() )。 JVM may namely keep running as long as the browser instance runs. 只要浏览器实例运行,JVM就可以继续运行。

When you hit Q... you are checking the contents of players but where is your call to destroy() ? 当您按下Q ...时,您正在检查players的内容,但是对destroy()调用在哪里? Do you explicitly call destroy() anywhere in your code? 您是否在代码中的任何地方显式调用destroy()

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

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