Java数组扩容实现方法解析
作者:呼呼睡觉睡觉啦 时间:2021-08-25 13:08:26
这篇文章主要介绍了Java数组扩容实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
第一种 int[] arr2=new int[arr1.length*2] //新数组的长度
第二种 int[] arr2=java.util.Arrays.copyOf(原数组名,新数组的长度);
第三种 int[] arr2=new int[arr1.length*2] System.arraycopy(原数组名,起始下标,新数组名,起始下标,复制长度);
小例
package com.xuefei.arrayTest;
/**
*
* @author 雪飞oubai
* 数组扩容
*/
public class BigArray {
public static int[] addArray(int[] arr1) {
int[] arr2=new int[arr1.length*2];
//新数组长度
for (int i = 0 ; i < arr1.length ; i++) {
arr2[i] = arr1[i];
}
return arr2;
}
public static void main(String[] args) {
int[] arr ={1,2};
int[] bigarr = addArray(arr);
System.out.println(bigarr.length);
}
}
来源:https://www.cnblogs.com/lililixuefei/p/11814126.html
标签:java,数组,扩容
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 添加、替换、删除PDF中的图片的示例代码
2023-08-28 09:06:26
![](https://img.aspxhome.com/file/2023/7/71637_0s.jpg)
java自定义切面增强方式(关于自定义注解aop)
2022-05-30 15:16:58
Springboot导出文件,前端下载文件方式
2023-07-21 11:27:05
![](https://img.aspxhome.com/file/2023/4/60774_0s.jpg)
Unity实现跑马灯抽奖效果
2022-10-09 04:09:54
![](https://img.aspxhome.com/file/2023/4/67054_0s.jpg)
详解C#如何实现读写ini文件
2022-02-04 23:15:27
![](https://img.aspxhome.com/file/2023/9/82139_0s.png)
spring web.xml指定配置文件过程解析
2023-05-15 01:32:40
Java使用MulticastSocket实现群聊应用程序
2021-09-23 12:12:29
![](https://img.aspxhome.com/file/2023/9/75669_0s.jpg)
java文件操作之Path,Paths,Files
2023-11-19 15:34:34
Java中的static关键字修饰属性和方法(推荐)
2021-09-29 05:46:20
![](https://img.aspxhome.com/file/2023/5/66035_0s.jpg)
使用java实现猜拳小游戏
2023-11-25 17:30:16
![](https://img.aspxhome.com/file/2023/4/79224_0s.jpg)
Maven引入外部jar的几种方法(小结)
2022-11-10 01:35:30
Java控制台实现猜拳游戏小游戏
2022-04-20 09:52:55
![](https://img.aspxhome.com/file/2023/6/80206_0s.jpg)
C#中foreach语句使用break暂停遍历的方法
2022-10-12 20:14:11
Java信号量Semaphore原理及代码实例
2021-10-02 21:55:08
学会使用C#异常
2021-08-17 23:31:19
![](https://img.aspxhome.com/file/2023/2/78192_0s.png)
C# Linq延迟查询的执行实例代码
2023-04-24 05:34:59
SpringBoot在Controller层接收参数的n种姿势(超详细)
2023-01-28 00:54:39
![](https://img.aspxhome.com/file/2023/0/60600_0s.jpg)
java使用Jdom实现xml文件写入操作实例
2022-09-30 02:38:23
feign 如何获取请求真实目的ip地址
2021-08-13 15:47:49
![](https://img.aspxhome.com/file/2023/1/76281_0s.jpg)
浅析java 10中的var关键字用法
2021-12-23 15:18:17