Java 未添加项目

Java 未添加项目,java,Java,当打印我的hashmap中的项目时[请参见以for Map.Entry开头的代码:WorldsByName.entrySet…),只有我添加的最后一个项目被打印出来。其他人消失了 ..... public interface World { abstract void run(String s); } private void sample(String[] inserted) { Map<

当打印我的hashmap中的项目时[请参见以for Map.Entry开头的代码:WorldsByName.entrySet…),只有我添加的最后一个项目被打印出来。其他人消失了

.....

        public interface World {
            abstract void run(String s);
        }

        private void sample(String[] inserted) {
            Map<String, World> WorldsByName = new HashMap<String, World>();
            WorldsByName.put(inserted[1], new World() {
                public void run(String s) {
                    if (inserted[0].equals("house")) {
                        System.out.println(inserted[0] + " with name " + s + " has been created.");

                    } else {
                        System.out.println("What do you mean by " + inserted[0] + " ?");
                    }
                }
            });

.....

您的代码只在WorldsByName映射中放置单个值

WorldsByName.put(inserted[1], new World() ...
所以,当您尝试执行for循环时,它将只执行一个键值

for (Map.Entry<String, World> entry : WorldsByName.entrySet()) {
从调用方方法传递实例化的WorldsByName,不要在sample方法中初始化它。确保为inserted和WorldsByName添加null检查和all in sample方法

要使WorldsByName成为全局变量:

Map<String, World> WorldsByName = new HashMap<String, World>();

private void sample(String[] inserted) {
    // Map<String, World> WorldsByName = new HashMap<String, World>();
    WorldsByName.put(inserted[1], new World() {...

如果要创建HashMapTest2类的多个实例,则不要将WorldsByName设置为静态,否则也可以将WorldsByName设置为静态

您可以尝试一次插入所有项,但该值将是对象签名

 static Map<String, World> WorldsByName = new HashMap<String, World>();

 private void sample(final String[] inserted) {

    for (int i = 0; i < inserted.length; i++)
        WorldsByName.put(inserted[i], new World() {
            public void run(String s) {
                if (inserted[0].equals("house"))
                    System.out.println(inserted[0] + " with name " + s
                            + " has been created.");
                else
                    System.out.println("What do you mean by " + inserted[0]
                            + " ?");
            }
        });
    for (Map.Entry<String, World> entry : WorldsByName.entrySet())
        System.out.println(entry.getKey() + ":-->" + entry.getValue());

  //..your other code here..
  }

并将该参数设为final,但我不确定您在这里究竟想要实现什么,除了这将以类似defparser.HashParser的格式为键插入一个值$1@b1c260

根据您之前的评论,我假设您只会通过两个参数,一个是house/hut/等,第二个是name。因此,您使用的是插入的[0]和插入的[1]。 只要只关心两个参数,就可以了。 要在映射中保留值,有多种方法: 1创建HashMap作为实例变量,并使用相同的对象调用sample方法。例如:

    class HashMaptest2{
        Map<String, World> worldsByname = new HashMap<String, World>();
        etc.
    }

第二种方法是在主类中创建HashMap,并将其传递给Naman指出的示例方法。

此代码应给出编译错误。。。如果在void runString s的实现中插入[0]。equalshouse,则不能在此处仅使用插入的[0]。inserted不是final。inserted数组包含什么,您是否尝试在示例方法中打印它?您必须将所有键放入映射和相应的值中。这是非常正确的。。。这只是在字符串数组的索引1处插入项目,不会产生任何问题,只需记下我在答案中的最后一句话。如果您发现该选项回答了您的问题,那么您可以通过单击复选标记来接受该选项。为什么要检查是否插入[0]。为什么HashMap中的每个值都是equalshouse,即为什么只有第0个位置?我要问@EagleOne,他在您的答案中添加的注释。Map WorldsByName是方法范围内的,因此,每当您将插入的新值传递到示例方法中时,它都会为WorldsByName创建新的HashMap,从而丢失以前的值。我这样做是因为以后我希望能够使用房屋的名称作为参考来绘制房屋。例如,当我输入command california red时,它会用名称california red来粉刷房子;如果我输入chicago blue,带钥匙的房子会被漆成蓝色等等。。。我想创建许多房屋,然后能够通过在hashmap中使用它们的密钥来修改它们。哦!HashMap正在被覆盖!
    class HashMaptest2{
        Map<String, World> worldsByname = new HashMap<String, World>();
        etc.
    }
psvm(){
HashMapTest2 test=new HashMaptest2();
test.sample(inserted1);
test.sample(inserted2)