我是 Java 的新手,在这里我试图尽量减少我编写的代码量。我想只使用一个 for 循环将元素添加到 2 个相同大小的数组中。我尝试了很多方法,但最终将相同的元素添加到两个数组中。如何循环两次并添加不同的元素?
这就是我使用两个for循环的方式......
public static void main (String[] args) throws IOException {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
int size = Integer.parseInt(reader.readLine());
int[] arr1 = new int[size];
int[] arr2 = new int[size];
String[] elem1 = reader.readLine().split("\\s+");
for(int i = 0; i < size; i++)
{
arr1[i] = Integer.parseInt(elem1[i]);
}
String[] elem2 = reader.readLine().split("\\s+");
for(int j = 0; j < size; j++)
{
arr2[j] = Integer.parseInt(elem2[j]);
}
System.out.println(LargestPair( arr1, arr2, size));
}