Java 如何获取输入以执行列表元素的求和?

Java 如何获取输入以执行列表元素的求和?,java,arraylist,Java,Arraylist,在这个问题中,我必须添加列表中的元素,并使用另一个列表打印它们。我认为我做错了,因为它给了我一个空的清单。我是java编程新手。请帮帮我 import java.util.ArrayList; 导入java.util.List; 导入java.util.*; 公共类源{ 公共列表GetSumofListents(第一个列表,第二个列表){ Listresult=新的ArrayList(); int i; 对于(i=0;i你可以做: Scanner scanner = new Scanner(Sy

在这个问题中,我必须添加列表中的元素,并使用另一个列表打印它们。我认为我做错了,因为它给了我一个空的清单。我是java编程新手。请帮帮我

import java.util.ArrayList;
导入java.util.List;
导入java.util.*;
公共类源{
公共列表GetSumofListents(第一个列表,第二个列表){
Listresult=新的ArrayList();
int i;
对于(i=0;i你可以做:

Scanner scanner = new Scanner(System.in);

String input1 = scanner.nextLine();
List<Integer> a = Arrays.stream(input1.split("[\\s,]+")).map(Integer::parseInt).collect(Collectors.toList());

String input2 = scanner.nextLine();
List<Integer> b = Arrays.stream(input2.split("[\\s,]+")).map(Integer::parseInt).collect(Collectors.toList());

List<Integer> result = new ArrayList<>();
for (int i = 0; i < a.size() && i < b.size(); i++) {
    result.add(a.get(i) + b.get(i));
}

for (int i = b.size(); i < a.size(); i++) {
    result.add(a.get(i));
}


System.out.print(result);
你可以做:

Scanner scanner = new Scanner(System.in);

String input1 = scanner.nextLine();
List<Integer> a = Arrays.stream(input1.split("[\\s,]+")).map(Integer::parseInt).collect(Collectors.toList());

String input2 = scanner.nextLine();
List<Integer> b = Arrays.stream(input2.split("[\\s,]+")).map(Integer::parseInt).collect(Collectors.toList());

List<Integer> result = new ArrayList<>();
for (int i = 0; i < a.size() && i < b.size(); i++) {
    result.add(a.get(i) + b.get(i));
}

for (int i = b.size(); i < a.size(); i++) {
    result.add(a.get(i));
}


System.out.print(result);

首先,主代码无法正确读取值,因为您提供了
nextInt
无法读取的非int(逗号)。我建议您将空格/逗号分隔的值写在一行上,然后拆分并解析为
int

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String[] firstValues = sc.nextLine().split("[\\s,]+"); // input: 10 , 20, 40, 30
    String[] secondValues = sc.nextLine().split("[\\s,]+");

    List<Integer> a = Stream.of(firstValues).map(Integer::parseInt).collect(Collectors.toList());
    List<Integer> b = Stream.of(secondValues).map(Integer::parseInt).collect(Collectors.toList());

    List<Integer> result1 = getSumOfListElements(a, b);
    System.out.print(result1);
}

最简单的多值输入

Scanner sc = new Scanner(System.in);

List<Integer> a = new ArrayList<>();
for (String s : sc.nextLine().split("[\\s,]+"))
    a.add(Integer.parseInt(s));

List<Integer> b = new ArrayList<>();
for (String s : sc.nextLine().split("[\\s,]+"))
    b.add(Integer.parseInt(s));

List<Integer> result1 = getSumOfListElements(a, b);
System.out.print(result1);
Scanner sc=新扫描仪(System.in);
列表a=新的ArrayList();
对于(字符串s:sc.nextLine().split([\\s,]+”)
a、 添加(整数.parseInt);
列表b=新的ArrayList();
对于(字符串s:sc.nextLine().split([\\s,]+”)
b、 添加(整数.parseInt);
列表结果1=GetSumofListents(a,b);
系统输出打印(结果1);

首先,主代码无法正确读取值,因为您提供了
nextInt
无法读取的非int(逗号)。我建议您将空格/逗号分隔的值写在一行,然后拆分并解析为
int

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String[] firstValues = sc.nextLine().split("[\\s,]+"); // input: 10 , 20, 40, 30
    String[] secondValues = sc.nextLine().split("[\\s,]+");

    List<Integer> a = Stream.of(firstValues).map(Integer::parseInt).collect(Collectors.toList());
    List<Integer> b = Stream.of(secondValues).map(Integer::parseInt).collect(Collectors.toList());

    List<Integer> result1 = getSumOfListElements(a, b);
    System.out.print(result1);
}

最简单的多值输入

Scanner sc = new Scanner(System.in);

List<Integer> a = new ArrayList<>();
for (String s : sc.nextLine().split("[\\s,]+"))
    a.add(Integer.parseInt(s));

List<Integer> b = new ArrayList<>();
for (String s : sc.nextLine().split("[\\s,]+"))
    b.add(Integer.parseInt(s));

List<Integer> result1 = getSumOfListElements(a, b);
System.out.print(result1);
Scanner sc=新扫描仪(System.in);
列表a=新的ArrayList();
对于(字符串s:sc.nextLine().split([\\s,]+”)
a、 添加(整数.parseInt);
列表b=新的ArrayList();
对于(字符串s:sc.nextLine().split([\\s,]+”)
b、 添加(整数.parseInt);
列表结果1=GetSumofListents(a,b);
系统输出打印(结果1);
试试这个:

while (sc.hasNextInt()) {
  int i = sc.nextInt();
  a.add(i);
}
sc.next(); // add this
while (sc.hasNextInt()) {
  int i = sc.nextInt();
  b.add(i);
}
sc.close();
在helper方法中,需要防止边界外的情况:

public List<Integer> getSumOfListElements(List<Integer> first, List<Integer> second){
  List<Integer> result = new ArrayList<>();
  int firstSize = first.size(), secondSize = second.size();
  for(int i = 0; i < firstSize || i < secondSize; i++){
    int firstVal = i < firstSize ? first.get(i) : 0,
      secondVal = i < secondSize ? second.get(i) : 0;
    result.add(firstVal + secondVal);
  }
  return result;
}
public List getsumof列表元素(先列表,后列表){
列表结果=新建ArrayList();
int firstSize=first.size(),secondSize=second.size();
for(int i=0;i
试试这个:

while (sc.hasNextInt()) {
  int i = sc.nextInt();
  a.add(i);
}
sc.next(); // add this
while (sc.hasNextInt()) {
  int i = sc.nextInt();
  b.add(i);
}
sc.close();
在helper方法中,需要防止边界外的情况:

public List<Integer> getSumOfListElements(List<Integer> first, List<Integer> second){
  List<Integer> result = new ArrayList<>();
  int firstSize = first.size(), secondSize = second.size();
  for(int i = 0; i < firstSize || i < secondSize; i++){
    int firstVal = i < firstSize ? first.get(i) : 0,
      secondVal = i < secondSize ? second.get(i) : 0;
    result.add(firstVal + secondVal);
  }
  return result;
}
public List getsumof列表元素(先列表,后列表){
列表结果=新建ArrayList();
int firstSize=first.size(),secondSize=second.size();
for(int i=0;i
谢谢你的帮助。事实上,我还没有开始研究收集器。但是这段代码适用于此输入,但没有通过其他测试用例。我一定是写错了什么,谢谢你的帮助。事实上,我还没有开始研究收集器。但是这段代码适用于此输入,但没有通过其他测试用例。我想I don’我没有写错什么东西,谢谢你。嘿,非常感谢你,我完全忘记了输入中的逗号。我对收集器不太了解,我必须研究一下。@Raeroxx刚刚编辑了我的帖子,添加了一个最简单的解决方案,没有收集器和流,可以读取多个值。请告诉我我输入的内容是否有误比如“10,20,30,40,50”那么,如何处理逗号和倒逗号呢?@Raeroxx为什么要在控制台中为输入编写倒逗号呢?事实上,它显示了我的部分更正,所以我认为可能与倒逗号有关,但我猜不是因为我在其他IDE中运行相同的代码,它工作得很好。顺便说一句,谢谢你的easiest解决方案。嘿,非常感谢你,我完全忘记了输入中的逗号。我对收集器不太了解,我必须研究一下。@Raeroxx刚刚编辑了我的帖子,添加了一个最简单的解决方案,没有收集器和流,阅读多个值。你可以告诉我我的输入是否类似于“10,20,30,40,50”那么,如何处理逗号和倒逗号呢?@Raeroxx为什么要在控制台中为输入编写倒逗号呢?事实上,它显示了我的部分更正,所以我认为可能与倒逗号有关,但我猜不是因为我在其他IDE中运行相同的代码,它工作得很好。顺便说一句,谢谢你的easi最好的解决方案。