欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java自动打包和解包结合泛型使用例子,java和解,上次写了一个Java入门

来源: javaer 分享于  点击 34304 次 点评:203

Java自动打包和解包结合泛型使用例子,java和解,上次写了一个Java入门


上次写了一个Java入门基础的关于Java自动打包和解包的小例子,但在这个例子中没有使用泛型,所以还有许多比较麻烦的地方,所以修改使用泛型后再次贴出这个例子。本代码来自本人博客:http://www.taoniwu.com/archives/2977.html

[Java]代码

package com.taoniwu;import java.util.*;public class Test_Auto_box {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        Map<String,Integer> m1 = new HashMap<String,Integer>();        Map<String,Integer> m2 = new HashMap<String,Integer>();        //给m1添加元素,在1.4以前使用,1.5以后有Auto_box可以自动打包        //m1.put("one",new Integer(1));        m1.put("one",1);        //m1.put("two",new Integer(2));        m1.put("two",2);        //m1.put("three",new Integer(3));        m1.put("three",3);        m2.put("A",1);        m2.put("B",2);        System.out.println(m1.size());        System.out.println(m1.containsKey("one"));        //System.out.println(m2.containsValue(new Integer(1)));        System.out.println(m2.containsValue(1));            if(m1.containsKey("two")){            //取出m1中索引为“two”的值,在1.4以前使用,1.5以后具有自动解包功能            //int i = ((Integer)m1.get("two")).intValue();            //使用泛型,不用强制转换,返回值为Integer类型,自动解包            //int i = (Integer)m1.get("two");            int i = m1.get("two");            System.out.println(i);        }        Map<String,Integer> m3 = new HashMap<String,Integer>(m1);        m3.putAll(m2);        System.out.println(m3);    }}
相关栏目:

用户点评