复制数组System.arraycopy的用法以及对随机数Math.random的一些思考

复制数组System.arraycopy的用法以及对随机数Math.random的一些思考

复制数组函数的定义

System.arraycopy(src, srcPos, dest, destPos, length)

src: 源数组
srcPos: 从源数组复制数据的起始位置
dest: 目标数组
destPos: 复制到目标数组的起始位置
length: 复制的长度

System.arraycopy(a, 0, b, 0, 3);
//把数组a的起始位复制到数组b起始位,复制的长度为3

如果不理解我们可以通过代码进一步进行理解

代码如下

package j2se;

public class FuZhiShuZu {

	public static void main(String[] args) {
		
		//复制数组
		int[] a = new int[] {12,13,54,34,76};
		int[] b = new int[3];//初始分配长度为3的空间,没有赋值
		
		//把数组a的前3位赋值给数组b
		
		//方法一:for循环
		for (int i = 0; i < b.length; i++) {
			b[i] = a[i];
		}
		
		//打印出来
		System.out.println("数组b的值为:");
		for (int i = 0; i < b.length; i++) {
			System.out.print(b[i] + " ");
		}
		System.out.println();
		
		//方法二:使用数组赋值函数
		System.arraycopy(a, 0, b, 0, 3);//把数组a的起始位复制到数组b起始位,复制的长度为3
		
		//打印出来
		System.out.println("数组b的值:");
		for (int i = 0; i < b.length; i++) {
			System.out.print(b[i] + " ");
		}
		System.out.println();
		
	}
}

利用函数System.arraycopy复制数组是不是很简单?
那我们来做一个小练习

复制数组的小练习

题目

准备两个数组,他俩的长度是5-10之间的随机数
并使用随机数初始化这两个数组
然后准备第三个数组,第三个数组的长度是前两个的和
通过System.arraycopy 把前两个数组合并到第三个数组中

题目答案如下,先做一遍,如果不会再看答案哟

package j2se;

public class HeBingShuZu {
	
	public static void main(String[] args) {
		
		//新建两个数组,长度为5-10的随机数
		//Math.random()产生的随机数的范围是 [0,1)(即取不到1这个值),
		//所以(int)(Math.random() * 5)+5 的取值范围是[5,10),达不到题目中的 5~10 的要求,
		//所以建议改成(int)((Math.random()*5.1) + 5)
		int m = (int)(Math.random() * 5.1) + 5;//m取值5-10
		int n = (int)(Math.random() * 5.1) + 5;
		//System.out.println(m);
		//System.out.println(Math.random() * 5.1);
		//[0,1)*5=[0,5)
		//[0,1)*5.1=[0,5.1)
		
		//用随机数初始化这两个数组,数组长度m,数组名a和b
		int[] a = new int[m];
		int[] b = new int[n];
		
		//随机数初始化两个数组
		for (int i = 0; i < a.length; i++) {
			a[i] = (int)(Math.random() * 100);//填充随机数0-99	
		}
		for (int i = 0; i < b.length; i++) {
			b[i] = (int)(Math.random() * 100);
		}
		
		//打印第一个数组
		System.out.println("c数组为:");
		for (int i = 0;i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
		
		//打印第二个数组
		System.out.println("b数组为:");
		for (int i = 0;i < b.length; i++) {
			System.out.print(b[i] + " ");
		}
		System.out.println();
		
		//准备第三个数组,长度是前两个数组之和,用函数合并到第三个数组
		int[] c = new int[m + n];
		System.arraycopy(a, 0, c, 0, m);
		System.arraycopy(b, 0, c, m, n);
		
		//打印第三个数组
		System.out.println("c数组为:");
		for (int i = 0;i < c.length; i++) {
			System.out.print(c[i] + " ");
		}
		System.out.println();
		
		//增强for循输出数组c
		System.out.println("增强for循环输出数组c:");
		for (int i : c) {
			System.out.print(i + " ");
		}
	}
}

对于随机数Math.random()函数的一些思考

[0,1)*5=[0,5)范围是0到4
[0,1)*5.1=[0,5.1)此时才可以取到5,范围是0到5

题目中要求的随机数是5-10
而Math.random()产生的随机数的范围是 [0,1)(即取不到1这个值)
(int)(Math.random() * 5) + 5 的取值范围是[5,10)达不到题目中的 5-10 的要求,
所以建议将其改成

(int) ((Math.random()*5.1) + 5)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值