如何将java已声明并实例化的数组,其下标在变长
java吧
全部回复
仅看楼主
level 9
bodanli159951 楼主
如:int score[] = new int[4];
变到其score.length显示为5。
求大神罩我
2013年04月07日 09点04分 1
level 9
score= new int[5];[扭扭]
2013年04月07日 09点04分 2
谢谢,请在看一下下面,麻烦你了
2013年04月07日 09点04分
level 9
bodanli159951 楼主
int score[] = new int[4] ;
score[0] = 0;
score[1] = 1;
score[2] = 2;
score[3] = 3;
score =new int[5];
score[4] = 4;
for (int i = 0 ; i < score.length ; i++){
System.out.print(score[i] + ",");
}
输出为0,0,0,0,4,
这个是开辟了新的对内存,有没有保留原来的地址不变,但长度增加?
或者有啥数组类的方法,可以针对这样用?
2013年04月07日 09点04分 3
我想快捷的输出原来已经初始化的内容,输出0,1,2,3,4
2013年04月07日 09点04分
不能,可以拷贝到新的数组里,System.arraycopy
2013年04月07日 09点04分
回复 cnfree2000 :就是说,只能开辟新的堆内存空间,是么?
2013年04月07日 09点04分
回复 bodanli159951 : 是的,考虑List吧。
2013年04月07日 10点04分
level 11
ArrayList<Integer>
2013年04月07日 09点04分 4
谢谢
2013年04月07日 10点04分
1