码迷,mamicode.com
首页 > 编程语言 > 详细

动态调整数组的长度

时间:2018-02-11 21:24:08      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:value   add   gpo   eof   des   color   star   rgs   body   

 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 

 

动态调整数组的长度

标签:value   add   gpo   eof   des   color   star   rgs   body   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!