java System.copyarray()

本文介绍Java中使用System.arraycopy()方法进行数组复制的过程。该方法能够高效地将一个数组的内容复制到另一个数组中,指定源数组的位置、目标数组的位置及复制元素的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java数组复制 System.copyarray()方法

System.copyarray()方法可以用来复制数组,其格式是:

System.arraycopy(Object src, int src_position, Object dst, int dst_position, int length)

它将数组从src 复制到dst,复制的位置是src 的第src_position 个元素到dst 的第dst_position

位置,复制元素的个数为length。

注意:该方法只复制元素。如果数组元素是引用型变量,则只复制引用,不复制对象

实体。

package com.gdcp; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class SortManager { private static final Scanner sc= new Scanner(System.in); //初始化的数组 private static int arrlength = 100; private static int[] initArray = new int[arrlength]; private static int[] resultArray; private static boolean isExitMain =false; private static boolean selectDataSource =true; private static boolean selectSort=true; //与性能相关的变量 private static int moveCount=0; private static int compareCount=0; private static long runTime; public static void main(String[] args) throws IOException { // System.out.println(); //快捷键sout int choice = 0; do { while (selectDataSource){ mainMenu(); choice = sc.nextInt(); switch (choice){ case 1: ascArray(); break; case 2: descArray(); break; case 3: randomArray(); break; case 4: scannerArray(); break; case 5: fileArray(); break; default: System.out.println("bye-bye! 欢迎再次使用排序器!"); break; } selectDataSource=false; } //排序方法的选择 while (selectSort){ sortMenu(); choice = sc.nextInt(); switch (choice){ case 1: System.out.println("排序前的数组:"); printArray(initArray); copyArray(); System.out.println("排序后的数组:"); straightInsertSort(); printArray(resultArray); System.out.println(); printXingneng(); break; case 2: System.out.println("排序前的数组:"); printArray(initArray); copyArray(); System.out.println("排序后的数组:"); package com.gdcp; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class SortManager { private static final Scanner sc= new Scanner(System.in); //初始化的数组 private static int arrlength = 100; private static int[] initArray = new int[arrlength]; private static int[] resultArray; private static boolean isExitMain =false; private static boolean selectDataSource =true; private static boolean selectSort=true; //与性能相关的变量 private static int moveCount=0; private static int compareCount=0; private static long runTime; public static void main(String[] args) throws IOException { // System.out.println(); //快捷键sout int choice = 0; do { while (selectDataSource){ mainMenu(); choice = sc.nextInt(); switch (choice){ case 1: ascArray(); break; case 2: descArray(); break; case 3: randomArray(); break; case 4: scannerArray(); break; case 5: fileArray(); break; default: System.out.println("bye-bye! 欢迎再次使用排序器!"); break; } selectDataSource=false; } //排序方法的选择 while (selectSort){ sortMenu(); choice = sc.nextInt(); switch (choice){ case 1: System.out.println("排序前的数组:"); printArray(initArray); copyArray(); System.out.println("排序后的数组:"); straightInsertSort(); printArray(resultArray); System.out.println(); printXingneng(); break; case 2: System.out.println("排序前的数组:"); printArray(initArray); copyArray(); System.out.println("排序后的数组:"); shellSort(); printArray(resultArray); System.out.println(); printXingneng(); break; case 3: System.out.println("排序前的数组:"); printArray(initArray); copyArray(); System.out.println("排序后的数组:"); straightSelectionSort(); printArray(resultArray); System.out.println(); printXingneng(); break; case 4: System.out.println("排序前的数组:"); printArray(initArray); copyArray(); System.out.println("排序后的数组:"); bubblingSort(); printArray(resultArray); System.out.println(); printXingneng(); break; case 5: printArray(initArray); break; case 6: printArray(initArray); break; default: System.out.println("bye-bye! 欢迎再次使用排序器!"); break; } selectSort=false; } //是否继续? continueMenu(); choice=sc.nextInt(); switch (choice){ case 1: isExitMain=true; break; case 2: selectDataSource=true; selectSort=true; break; case 3: selectDataSource=false; selectSort=true; break; } }while (!isExitMain); } private static void printXingneng(){ System.out.println("移动次数:"+moveCount+"\t"); System.out.println("比较次数:"+compareCount+"\t"); System.out.println("运行时间:"+runTime); } private static void shellSort(){ long startTime = System.nanoTime(); moveCount =0; compareCount =0; runTime =0; //定义一些变量 int i,j,temp; //定义增量 int gap; for (gap=arrlength/2; gap>=1; gap/=2) { compareCount++; for (i=gap;i<arrlength;i++){ j=i-gap; temp=initArray[i]; compareCount++; moveCount++; while ((j>=0) && (resultArray[j]>temp)) { initArray[j+gap]=resultArray[j]; j-=gap; compareCount+=2; moveCount++; } resultArray[j+gap]=temp; moveCount++; } } runTime=System.nanoTime()-startTime; } //复制数组 private static void copyArray(){ resultArray=new int[arrlength]; for(int i=0;i<resultArray.length;i++){ resultArray[i]=initArray[i]; } } private static void straightInsertSort(){ long startTime = System.nanoTime(); moveCount = 0; compareCount = 0; runTime = 0; int i,j,temp; for(i=1;i<arrlength;i++){ temp = resultArray[i]; j=i-1; compareCount++; moveCount++; while(j>=0 && resultArra
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值