赞助
posts - 449,comments - 12,views - 11万

创建数组时,必须显示指定长度,并在创建之后不可更改长度

扩容的思路:

创建大于原数组长度的新数组

将原数组中的元素依次复制到新数组中

 

 

复制方式 

循环将原数组中所有元素逐一赋值给新数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pubilc class Test{
  public static void main(String[] args){
  int [] nums = new int[5]
  nums[0] = 1
 nums[0] = 2
nums[0] =3
 nums[0] = 4
 nums[0] = 5
//创建比原数组还大的数组
int [] bigArr = new int[nums.length + 2];//*2
for(int i = 0;i<nums.length;i++){
System.out.print(nums[i])
 bigArr[i] = nums[i]
}
}
}

  System.arraycopy(原数组,原数组起始,新数组,新数组起始,长度)

1
2
3
4
5
6
7
8
9
pubilc class Test{
  public static void main(String[] args){
  int [] nums = new int[5]
  nums[0] = 1
  nums[0] = 2
  nums[0] =3
  nums[0] = 4
  nums[0] = 5
//创建比原数组还大的数组int [] newsArr = new int[];int [] newsArrs = System.arraycopy(nums,0,newsArr,0,nums.length * 2) } } 

  java.util.Arrays.copyOf(原数组,新长度);//返回带有原值的新数组

1
2
3
4
5
6
7
8
9
10
11
12
pubilc class Test{
  public static void main(String[] args){
  int [] nums = new int[5]
  nums[0] = 1
  nums[0] = 2
  nums[0] =3
  nums[0] = 4
  nums[0] = 5
//创建比原数组还大的数组
int [] newsArr = java.util.Arrays.copyOf(nums,nums.length * 2);
}
}

  

posted on   Tsunami黄嵩粟  阅读(89)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2021-04-16 react减少组件渲染
2021-04-16 react生命周期比较常用的几个
2021-04-16 react做购物车的功能
2021-04-16 react表单处理 非受控组件
2021-04-16 react表单处理 受控组件
2021-04-16 react props-type
2021-04-16 react props进阶 children属性
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示