动态调整数组的长度
1 package com.jdk7.chapter4; 2 3 public class AdjustArrayLength { 4 private static int ADD_LENGTH = 10; 5 public static Integer[] addArrayLength(Integer[] src){ 6 return addArrayLength(src,ADD_LENGTH); 7 } 8 9 public static Integer[] addArrayLength(Integer[] src, int length){ 10 if(src==null){ 11 return null; 12 } 13 Integer[] dest = new Integer[src.length+length]; 14 System.arraycopy(src, 0, dest, 0, src.length); 15 return dest; 16 } 17 18 public static void printArray(Integer[] obj){ 19 for(int i=0;i<obj.length;i++){ 20 System.out.print(obj[i].toString()+" "); 21 } 22 System.out.println(); 23 } 24 25 public static void main(String[] args) { 26 Integer[] array = new Integer[10]; 27 for(int i=0;i<10;i++){ 28 array[i] = Integer.valueOf(i); 29 // array[i] = new Integer(i); 30 } 31 32 Integer[] initArray = AdjustArrayLength.addArrayLength(array); 33 // Integer[] initArray = AdjustArrayLength.addArrayLength(array, 5); 34 for(int i=10;i<initArray.length;i++){ 35 initArray[i] = Integer.valueOf(i); 36 } 37 AdjustArrayLength.printArray(initArray); 38 } 39 } 40 41 执行结果: 42 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19