Java 将容器 Map中的内容保存到数组


import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;

public class mapToArr {
    
    public static void main(String[] args) {
        
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put( "a", 11 );
        map.put( "b", 23 );
        map.put( "c", 68 );
        System.out.println( "map=" + map.toString() );
        
        int count = 0;
        Integer intArr[] = new Integer[map.size()];
        
        // 方法一
//        for( String key : map.keySet() ) {
//            intArr[count] = map.get(key);
//            count++;
//        }
        
        // 方法二
        for( Entry<String, Integer> entry : map.entrySet() ) {
            intArr[count] = entry.getValue();
            count++;
        }
        
        for( int i = 0; i < intArr.length; i++ ) {
            System.out.print( "intArr[" + i + "]=" + intArr[i] + "  " );
        }
        
    }

}

 

 

运行结果为:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM