java中Map和List初始化的兩種方法


 1 //初始化List
 2 List<string> list = new ArrayList</string><string>();
 3 list.add("string1");
 4 list.add("string2");
 5 //some other list.add() code......
 6 list.add("stringN");
 7  
 8 //初始化Map
 9 Map<string object=""> map = new HashMap<string object="">();
10 map.put("key1", "value1");
11 map.put("key2", "value2");
12 //.... some other map.put() code
13 map.put("keyN", "valueN");
14 </string></string></string>

 

 1 //初始化List
 2     List<string> list = new ArrayList<string>(){{
 3         add("string1");
 4         add("string2");
 5         //some other add() code......
 6         add("stringN");
 7     }};
 8  
 9     //初始化Map
10     Map<string string=""> map = new HashMap<string object="">(){{
11         put("key1", "value1");
12         put("key2", "value2");
13         //.... some other put() code
14         put("keyN", "valueN");
15     }};
ArrayList<string> list = new ArrayList(Arrays.asList("Tom", "Jerry", "Mike"));
List list = new ArrayList<string>(){{
    add("A");
    add("B");
}}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM