首页 > 编程语言 > 详细

动态调整数组的长度

时间:2018-02-11 20:45:57      阅读:200      评论:0      收藏:0      [点我收藏+]
 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 

 

动态调整数组的长度

原文:https://www.cnblogs.com/celine/p/8443071.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!