英文:
Creating multiple arrays using one for loop in Java
问题
我是 Java 的新手,在这里我试图减少我编写的代码量。
我想要使用只有一个 for 循环将元素添加到两个大小相同的数组中。我尝试了很多方法,但结果是向这两个数组中同时添加了相同的元素。如何使我能循环两次并添加不同的元素?
以下是我使用两个 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));
}
英文:
I'm a newbie to Java and here I am trying to minimize the amount of code I've written.
I want to add elements to 2 arrays of the same size using only one for loop. I tried many ways but it ended up adding the same elements to both arrays. How can I loop twice and add different elements?
This's how I did it using two for loops...
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));
}
答案1
得分: 1
String[] elem1 = reader.readLine().split("\s+");
String[] elem2 = reader.readLine().split("\s+");
for(int i = 0; i < size; i++)
{
arr1[i] = Integer.parseInt(elem1[i]);
arr2[i] = Integer.parseInt(elem2[i]);
}
英文:
String[] elem1 = reader.readLine().split("\\s+");
String[] elem2 = reader.readLine().split("\\s+");
for(int i = 0; i < size; i++)
{
arr1[i] = Integer.parseInt(elem1[i]);
arr2[i] = Integer.parseInt(elem2[i]);
}
答案2
得分: 0
以下是已翻译的内容:
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) throws IOException {
System.out.print("输入数组大小:");
int size = Integer.parseInt(SCANNER.nextLine());
Integer[] arr1 = getAsIntArray(size);
Integer[] arr2 = getAsIntArray(size);
System.out.println(最大对(arr1, arr2, size));
}
private static Integer[] getAsIntArray(int size) {
System.out.print("输入" + size + "个以空格分隔的元素:");
return Arrays.stream(SCANNER.nextLine().split("\\s+"))
.map(Integer::parseInt)
.collect(Collectors.toList())
.toArray(Integer[]::new);
}
你是想表达这个意思吗?
英文:
Something like the following should work:
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) throws IOException {
System.out.print("Enter size of array: ");
int size = Integer.parseInt(SCANNER.nextLine());
Integer[] arr1 = getAsIntArray(size);
Integer[] arr2 = getAsIntArray(size);
System.out.println(LargestPair(arr1, arr2, size));
}
private static Integer[] getAsIntArray(int size) {
System.out.print("Enter " + size + " space separated elements: ");
return Arrays.stream(SCANNER.nextLine().split("\\s+"))
.map(Integer::parseInt)
.collect(Collectors.toList())
.toArray(Integer[]::new);
}
Is this what you meant?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论