Java数组参数应用&数组间数值覆盖"以数组间数值覆盖实验为例"

Q11. Write a method named copyRange that takes as parameters two arrays a1 and a2, two starting indexes i1 and i2, and a length l,
and copies the first l elements of a1 starting at index i1 into array a2
starting at index i2.

For example, if the following arrays are declared:

int[] a1 = {10, 20, 30, 40, 50, 60};
int[] a2 = {91, 92, 93, 94, 95, 96};
copyRange(a1, a2, 0, 2, 3);
After the preceding call, the contents of a2 would be
{91, 92, 10, 20, 30, 96}. You may assume that the parameters’
values are valid, that the arrays are large enough to hold
the data, and so on.

import java.util.*;
import java.util.Scanner;
public class Q11 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner console = new Scanner (System.in);
		System.out.println("Type the length of array a1: ");
		int[]a1= new int[console.nextInt()];
		System.out.println("Type the length of array a2: ");
		int []a2= new int [console.nextInt()];
		System.out.println("Type the starting index1: ");
		int index1 = console.nextInt();
		System.out.println("Type the starting index2: ");
		int index2 = console.nextInt();
		System.out.println("Type the length: ");
		int length = console.nextInt();
		for (int i=0;i<a1.length;i++) {
			System.out.println("Type the #"+(i+1)+" for a1");
			a1[i]=console.nextInt();
		}
		for (int a=0;a<a2.length;a++) {
			System.out.println("Type the #"+(a+1)+" for a2");
			a2[a]=console.nextInt();
		}
		int []a3 = copeRange(a1,a2,index1,index2,length);
		System.out.println(Arrays.toString(a3));
		
	}
	public static int [] copeRange(int[]num1,int []num2,int num3, int num4,int num5 ){
		int[]array= num2;
		//建立新数组 储存num2的值
		for (int i =num3;i<=nu
		m3+num5-1;i++) {
			array[num4+(i-num3)]=num1[i];
		}
		return array;
	}
}


你可能感兴趣的:(Java)