用一个for循环在Java中创建多个数组

huangapple go评论137阅读模式
英文:

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(&quot;\\s+&quot;);
        for(int i = 0; i &lt; size; i++)
        {
            arr1[i] = Integer.parseInt(elem1[i]);
        }


        String[] elem2 = reader.readLine().split(&quot;\\s+&quot;);
        for(int j = 0; j &lt; 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(&quot;\\s+&quot;);
String[] elem2 = reader.readLine().split(&quot;\\s+&quot;);
for(int i = 0; i &lt; 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(&quot;Enter size of array: &quot;);
        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(&quot;Enter &quot; + size + &quot; space separated elements: &quot;);
        return Arrays.stream(SCANNER.nextLine().split(&quot;\\s+&quot;))
                .map(Integer::parseInt)
                .collect(Collectors.toList())
                .toArray(Integer[]::new);
    }

Is this what you meant?

huangapple
  • 本文由 发表于 2020年10月23日 21:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64501206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定