• 数组的扩容


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

    扩容的思路:

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

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

    复制方式 

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

    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(原数组,原数组起始,新数组,新数组起始,长度)

    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(原数组,新长度);//返回带有原值的新数组

    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);
    }
    }
    

      

  • 相关阅读:
    发短信集合类-阿里云短信涉及类
    第四方 fast快捷支付封装
    佰米支付封装
    支付宝支付封装【修改至2021.01.11】
    关于支付
    tp5下的文件上传与下载类
    发送短信集合类
    文件中设置开启访问权限
    SpringBoot注解分析
    HashMap底层实现原理及面试问题
  • 原文地址:https://www.cnblogs.com/ht955/p/16153989.html
Copyright © 2020-2023  润新知