我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*filename:P6Q13
*Name:Wang Chaoran
Desciprtion:
13 (Increasing array size)
Once an array is created, its size is fixed. Occasionally,
you need to add more values to an array, but it is full.
In such cases, you can create a new, larger array to replace the existing array.
Write a method with the following header:
public static int[] doubleCapacity(int[] list)
The method returns a new array that doubles the size of the parameter list.
*/
class P6Q13{
public static void main(String[] args){
int[] test = {0,1,2,3,4,5,6,7,8,9};
int[] array = doubleCapacity(test);
System.out.println("The length of the new array: "+array.length);
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
}
public static int[] doubleCapacity(int[] list){
int[] array = new int[list.length*2];
for(int i = 0;i<list.length;i++)
array[i]=list[i];
return array;
}
}
导入论坛查看(32)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing