请在 Solution
类中定义一个非静态方法 copy()
,用来把任意参数类型的一个数组中的数据安全地复制到相应类型的另一个数组中,并且不用指定方法的返回值。
调用copy
方法,并传递两个参数,第一个参数是原数组(有值),第二个是目标数组(无值),将第一个数组中的值复制到第二数组中。
样例一
当传入第一个数组为 [1, 2, 3, 4, 5]
时,执行完 copy()
方法后第二个数组的值为:
[1, 2, 3, 4, 5]
样例二
当传入第一个数组为 [a, b, c, d, e]
时,执行完 copy()
方法后第二个数组的值为:
[a, b, c, d, e]
解析:
Main.java
import java.io.FileReader;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
String inputPath = args[0];
String outputPath = args[1];
Scanner in = new Scanner(new FileReader(inputPath));
PrintStream ps = new PrintStream(outputPath);
// get data
String str1 = in.nextLine();
String strArr1[] = str1.substring(str1.indexOf("[") + 1, str1.indexOf("]")).split(",");
for (int i = 0; i < strArr1.length; i++) {
strArr1[i] = strArr1[i].trim();
}
String strArr2[] = new String[strArr1.length];
// Validation Method Type
Solution solution = new Solution();
solution.copy(strArr1, strArr2);
StringBuilder sb = new StringBuilder("[");
for (String str : strArr2) {
sb.append(str + ", ");
}
sb.delete(sb.length()-2, sb.length());
sb.append("]");
ps.print(sb.toString());
ps.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Solution.java
import java.util.*;
public class Solution {
public <T> T[] copy(T[] a,T[] b){
for (int i= 0;i<a.length;i++){
b[i] = a[i];
};
return b;
}
}