在Map<中插入值K,V> [英] Inserting values in Map< K, V >

查看:163
本文介绍了在Map<中插入值K,V>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个应用程序,我正在尝试创建一个存储一些信息的地图。

I'm programming my first app, and I am trying to create a Map that will store some information.

我正在使用ECLIPSE并且我一直在使用ECLIPSE相同的错误,但我有Googled(实际上是DuckDuckGo-ed)并在StackOverflow中搜索,我找不到错误...所以我拼命地来到这里。

I'm using ECLIPSE and I keep getting the same error, but I have Googled (actually DuckDuckGo-ed) and search in StackOverflow and I can't find the error... So I've desperately come here.

正确导入库(因为ADT会这样做)而且我猜测Oracle文档是可以的,所以...... 为什么它不起作用?

Libraries are imported correctly (since the ADT does so) and I'm guessing Oracle documentation is okay, so... why isn't it working?

我的意思是,这两种方法都不能将Key-Value对插入到地图中。

I mean, neither of both methods work to insert a Key-Value pair into the map.

查看错误时会出现一些有趣的部分。

The interesting part comes when looking at the errors.

第一个说令牌上的语法错误,,{此标记后的预期

第二个说语法错误,插入}以完成ClassBody

我开始有点绝望了,我无法使代码更简单!
非常感谢!

I am beginning to get a bit desperate, I couldn't make the code simpler! Thank you very much!

推荐答案

这里有两个问题。首先,你必须将它放入一个方法,其次,你不能使用 [] 语法;你必须使用 Map.put

There are two issues here. First you must put this into a method, and second, you can't use the [] syntax; you must use Map.put.

 public class Data {
    public static void main(String[] args) {
      Map<String, String> data = new HashMap<String, String>();
      data.put("John", "Taxi Driver");
      data.put("Mark", "Professional Killer");
    }
 }

这篇关于在Map&lt;中插入值K,V>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆