当前位置: 首页 > news >正文

作业:复制数组

题目:

复制数组int[] arr={1,2,3,4,5}; int[] arr1={6,7,8,9,10}; int[] arrcopy={1,2,3,9,10} 也有可能 {6,2,3,4,10}。

代码:

//复制数组:
package java02;
import java.util.Random;
public class copyArray {public static void main(String[] args) {int[] arr1 = {1, 2, 3, 4, 5};int[] arr2 = {6, 7, 8, 9, 10};int[] arr3 = new int[arr1.length + arr2.length];//将两个数组(arr1和arr2)拼接成一个新的数组(arr3):for(int i = 0; i < arr1.length; i++) {arr3[i] =arr1[i];}for(int i = 0; i < arr2.length; i++) {arr3[arr1.length + i] = arr2[i];}//多打乱几次 arr3 数组:for(int i = 0; i < arr3.length; i++) {arr3 = disturbArray(arr3);}//得到需要复制的数组:int[] arrCopy = new int[arr1.length];for(int i = 0; i < arrCopy.length; i++) {arrCopy[i] = arr3[i];}//打印复制出来的数组 arrCopy 元素:System.out.print("{");for(int i = 0; i < arrCopy.length - 1; i++) {System.out.print(arrCopy[i] + ", ");}System.out.print(arrCopy[arrCopy.length - 1] + "}");}//定义一个方法用于打乱数组:public static int[] disturbArray(int[] arr3) {Random r = new Random();int r1 = r.nextInt(arr3.length);int r2 = r.nextInt(arr3.length);int temp = arr3[r1];arr3[r1] = arr3[r2];arr3[r2] = temp;return arr3;}
}

运行结果:


文章转载自:
http://antituberculous.tmizpp.cn
http://carrollian.tmizpp.cn
http://accoucheur.tmizpp.cn
http://bashlyk.tmizpp.cn
http://afterripening.tmizpp.cn
http://amaranthine.tmizpp.cn
http://aquaemanale.tmizpp.cn
http://allose.tmizpp.cn
http://cdplay.tmizpp.cn
http://amadan.tmizpp.cn
http://alexandrine.tmizpp.cn
http://bringdown.tmizpp.cn
http://ceremonially.tmizpp.cn
http://buck.tmizpp.cn
http://basilary.tmizpp.cn
http://bedu.tmizpp.cn
http://chanukah.tmizpp.cn
http://beano.tmizpp.cn
http://brent.tmizpp.cn
http://boater.tmizpp.cn
http://arbitration.tmizpp.cn
http://aprism.tmizpp.cn
http://behove.tmizpp.cn
http://acajou.tmizpp.cn
http://adze.tmizpp.cn
http://attempt.tmizpp.cn
http://asturias.tmizpp.cn
http://carny.tmizpp.cn
http://bayadere.tmizpp.cn
http://availably.tmizpp.cn
http://www.dtcms.com/a/281211.html

相关文章:

  • EndNote
  • 【Keil】C/C++混合编程的简单方法
  • DGNNet:基于双图神经网络的少样本故障诊断学习模型
  • 深入浅出 RabbitMQ-核心概念介绍与容器化部署
  • Element plus参考vben逻辑实现的描述列表组件封装实践
  • 【PTA数据结构 | C语言版】二叉树前序序列化
  • 差分信号接口选型指南:深入解析LVDS、SubLVDS、SLVDS与SLVDS-EC**
  • 《大数据技术原理与应用》实验报告五 熟悉 Hive 的基本操作
  • [AI8051U入门第三步]串口1使用-printf重定向(乱码解决办法)
  • Django+Celery 进阶:动态定时任务的添加、修改与智能调度实战
  • Android target34升级到35中的edge-to-edge适配
  • Nestjs框架: 数据库架构设计与 NestJS 多 ORM 动态数据库应用与连接池的配置
  • 利用android studio,对图片资源进行二次压缩
  • 基于Ruoyi和PostgreSQL的统一POI分类后台管理实战
  • 三步把餐饮回访差评变口碑
  • java+vue+SpringBoot在线租房和招聘平台(程序+数据库+报告+部署教程+答辩指导)
  • Ajax原理、用法与经典代码实例
  • TCP协议可靠性设计的核心机制与底层逻辑
  • 基于YOLOv8的水稻叶片病害检测系统的设计与实现【近6W条数据集+多病害特征+高准确率】
  • 修改系统配置后,如何编写 Python 脚本以适应 SSL 证书验证的变化
  • Axios 和 Promise 区别对比
  • C语言---自定义类型(下)(枚举和联合类型)
  • 利用DeepSeek为chdb命令行客户端添加输出重定向和执行SQL脚本功能
  • nlp论文:分本分类:《Bag of Tricks for Efficient Text Classification》
  • VirtualBox网络配置全指南:桥接、Host-Only与双网卡实战
  • 2025华为ODB卷-士兵过河-三语言题解
  • 限制apk使用时长第二篇-限制/拦截Apk启动-应用锁功能
  • 创客匠人:创始人 IP 变现,从 “单点尝试” 到 “生态赋能” 的跨越
  • S7-200 SMART PLC:不同CPU及数字量 IO 接线全解析
  • ESLint 配置错误:ReferenceError: prettier is not defined 解决方案