发布于 2016-07-27 21:16:15 | 102 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java并发编程示例,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要介绍了Java中动态地改变数组长度及数组转map的代码分享,其中转Map利用到了java.util.Map接口,需要的朋友可以参考下

动态改变数组的长度


/** * Reallocates an array with a new size, and copies the contents  
 * * of the old array to the new array.  
 * * @param oldArray the old array, to be reallocated.  
 * * @param newSize  the new array size.  
 * * @return     A new array with the same contents.  
 * */  
private static Object resizeArray (Object oldArray, int newSize) {    
  int oldSize = java.lang.reflect.Array.getLength(oldArray);    
  Class elementType = oldArray.getClass().getComponentType();    
  Object newArray = java.lang.reflect.Array.newInstance(       
      elementType,newSize);    
  int preserveLength = Math.min(oldSize,newSize);    
  if (preserveLength > 0)      
    System.arraycopy (oldArray,0,newArray,0,preserveLength);    
  return newArray;  }    
// Test routine for resizeArray().   
public static void main (String[] args) {    
  int[] a = {1,2,3};    
  a = (int[])resizeArray(a,5);    
  a[3] = 4;    
  a[4] = 5;    
  for (int i=0; i<a.length; i++)      
    System.out.println (a[i]);   
} 

代码只是实现基础方法,详细处理还需要你去Coding哦>>

把 Array 转换成 Map


import java.util.Map;   
import org.apache.commons.lang.ArrayUtils;    
public class Main {     
  public static void main(String[] args) {     
    String[][] countries = { { "United States", "New York" },  
        { "United Kingdom", "London" },       
        { "Netherland", "Amsterdam" },  
        { "Japan", "Tokyo" },  
        { "France", "Paris" } };      
    Map countryCapitals = ArrayUtils.toMap(countries);      
    System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));     
    System.out.println("Capital of France is " + countryCapitals.get("France"));    
}   
} 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务