国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java中動態地改變數組長度及數組轉Map的代碼實例分享

2019-11-26 14:29:18
字體:
來源:轉載
供稿:網友

動態改變數組的長度

/** * 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"));    }   } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舞钢市| 株洲县| 汤原县| 岱山县| 罗山县| 蓝田县| 乌兰县| 宁陵县| 通道| 闻喜县| 东明县| 金华市| 北辰区| 刚察县| 阿坝| 保康县| 五常市| 台中县| 泰和县| 霍林郭勒市| 温州市| 大兴区| 哈巴河县| 平顺县| 乌恰县| 宁安市| 常州市| 称多县| 昌黎县| 怀柔区| 巴彦县| 蚌埠市| 涟水县| 南宫市| 思茅市| 太湖县| 洛阳市| 太和县| 盘山县| 集安市| 林甸县|