数组拷贝操作 从数组默认下标 到拷到我指定的长度

package com.heima.array;


public class AarryDemo3 {


public static void main(String[] args) {
/*
* 1.定义一个方法copyOf(int[] arr, int newLength),
* 功能:将数组arr中的newLength个元素拷贝到新数组中, 并将新数组返回, 从索引为0开始
*/


int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // 定义一个新的数组
getInfo(arr);// 遍历数组
arr = copyOf(arr, 5); // 给一个数组 给一个你要拷贝的长度 返回一个新的数组
System.out.println("拷贝后的数组是");
getInfo(arr);
}
       //拷贝数组元素
public static int[] copyOf(int[] arr, int newLength) {
int[] newarr = new int[newLength]; // 定义一个数组给
for (int i = 0; i < newLength; i++) { //循环从数组下标0拷贝到newLength这个下标为值
newarr[i] = arr[i]; //把原始数组中取出来放入新数组中进去
}
return newarr; //返回一个数组
}
      //遍历数组中的元素
public static void getInfo(int[] arr) {
for (int i : arr) {
if (i > 0) {
System.out.print(i + " ");
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟子涵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值