java 数组随机组合,java练习:合并数组、生成随机数

import java.util.Random;

public class Example3 {

public static void main(String[] args) {

int aLength = getRandom(5,10);

int bLength = getRandom(5,10);

int a [] = new int[aLength];

int b [] = new int[bLength];

int c [] = new int[aLength+bLength];

for(int i = 0 ; i < aLength ; i++){

a[i] = (int) (Math.random() * 100);

}

for(int i = 0 ; i < bLength ; i++){

b[i] = (int) (Math.random() * 100);

}

System.out.println("a:");

//把内容打印出来

for (int i = 0; i < a.length; i++) {

System.out.print(a[i] + " ");

}

System.out.println("\nb:");

//把内容打印出来

for (int i = 0; i < b.length; i++) {

System.out.print(b[i] + " ");

}

//方法二: System.arraycopy(src, srcPos, dest, destPos, length)

//src: 源数组

//srcPos: 从源数组复制数据的起始位置

//dest: 目标数组

//destPos: 复制到目标数组的启始位置

//length: 复制的长度

System.arraycopy(a, 0, c, 0, aLength);

System.arraycopy(b, 0, c, aLength, bLength);

System.out.println("\nc:");

//把内容打印出来

for (int i = 0; i < c.length; i++) {

System.out.print(c[i] + " ");

}

}

public static int getRandom(int min, int max){

Random random = new Random();

int s = random.nextInt(max) % (max - min + 1) + min;

return s;

}

}

b8b1b215a0d48a81dcfd5c21a50989da.png

标签:java,随机数,int,System,length,数组,aLength,out

来源: https://www.cnblogs.com/pangchunlei/p/14172772.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值