一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java中動態地改變數組長度及數組轉Map的代碼實例分享

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

2020-04-11 14:37Ai2015WER JAVA教程

這篇文章主要介紹了Java中動態地改變數組長度及數組轉map的代碼分享,其中轉Map利用到了java.util.Map接口,需要的朋友可以參考下

動態改變數組的長度

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/** * 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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"));   
}  
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91人人在线 | 亚洲精品国产成人7777 | 国产成人手机在线好好热 | 疯狂刺激的3p国产在线 | 办公室操秘书 | 奶茶视频有容乃大 | 亚洲老头与老太hd | 精品午夜寂寞黄网站在线 | 国产综合久久久久 | 吃大胸寡妇的奶 | 骚虎最新网址 | 国产趴着打光屁股sp抽打 | 亚洲精品久久中文字幕 | 天天操天天爽天天射 | 亚洲精品国产国语 | 韩国三级理韩国三级理人伦 | 免费观看欧美性一级 | 亚洲国产货青视觉盛宴 | 我和黑色丝袜班主任 | 变态女王麻麻小说在线阅读 | hd在线观看免费高清视频 | 四虎麻豆| 精品视频免费在线 | 果冻传媒在线视频观看免费 | 女性全身裸露无遮挡 | 午夜免费体验30分 | 亚洲国产区 | 四虎最新紧急更新地址 | 午夜国产福利视频一区 | 国产大秀视频 | 魔法满屋免费观看完整版中文 | 超级碰碰免费视频 | 爽好舒服宝贝添奶吻戏 | 亚洲色图欧美偷拍 | 成人亚洲欧美日韩在线观看 | ass韩国美女人体pics | 香蕉免费看一区二区三区 | 91精品国产综合久久香蕉 | 91po国产在线高清福利 | 男人添女人 | 日本黄色影院 |