java 一步法在HashMap中声明并放置String数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12732806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declare and Put String array in HashMap in one step
提问by jule64
I am trying to insert static data into a HashMapin Java like this:
我正在尝试将静态数据插入到Java 中的HashMap中,如下所示:
HashMap<String,String[]> instruments = new HashMap<String, String[]>();
instruments.put("EURUSD", {"4001","EURUSD","10000","0.00001","0.1","USD"});
But the compiler doesn't like it. The only way I found to insert that data into the HashMap is to declare the string array separately and then put it into the HashMap, like this
但是编译器不喜欢它。我发现将该数据插入 HashMap 的唯一方法是单独声明字符串数组,然后将其放入 HashMap,就像这样
String[] instruDetails = {"4001","EURUSD","10000","0.00001","0.1","USD"};
instruments.put("EURUSD", instruDetails);
But it not very expressive, and hard to maintain
但它不是很有表现力,并且难以维护
So my question is, is there a way to do the put()
operation and string array declaration in one step/line?
所以我的问题是,有没有办法put()
在一个步骤/一行中完成操作和字符串数组声明?
回答by Baz
This will do it:
这将做到:
instruments.put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
回答by Rohit Jain
To get it all in one sentence, use double-braces initialization: -
要在一句话中了解所有内容,请使用双大括号初始化:-
HashMap<String,String[]> instruments = new HashMap<String, String[]>() {
{
put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
put("EUR", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
}
};
回答by Bhesh Gurung
I think you already got what works. But the reason that
我想你已经得到了有效的方法。但原因是
instruments.put("EURUSD", {"4001","EURUSD","10000","0.00001","0.1","USD"});
doesn't work is because {"4001","EURUSD","10000","0.00001","0.1","USD"}
. {}
is a syntactic sugar or short-cut in Java array for initialization. It comes with a constraint that it always has to go along with the array declaration statement, otherwise it's a syntax error.
不起作用是因为{"4001","EURUSD","10000","0.00001","0.1","USD"}
. {}
是 Java 数组中用于初始化的语法糖或捷径。它带有一个约束,它必须始终与数组声明语句一起使用,否则就是语法错误。
Array declaration statement like
数组声明语句如
String[] array = {"1", "2"};
That way Java knows that the array that it needs to create for you is actually of String
type elements.
这样 Java 就知道它需要为您创建的数组实际上是String
元素类型。
If you break the above statement as follows
如果你打破上面的陈述如下
String[] array;
array = {"1", "2"};
It doesn't compile.
它不编译。
And with the new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"}
, the compiler knows that it has to instantiate a new array which element type is String
(new String[]
) and initialize the newly instantiated array with values you provided ({"4001","EURUSD","10000","0.00001","0.1","USD"}
).
使用new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"}
,编译器知道它必须实例化一个元素类型为String
( new String[]
) 的新数组,并使用您提供的值( ) 初始化新实例化的数组{"4001","EURUSD","10000","0.00001","0.1","USD"}
。