java 如何将变量添加到数组列表,然后总计数组列表的所有元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7032478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add variable to array list and then total all the elements of arraylist?
提问by Dhruv
I'm trying to input a variable into an ArrayList and then add all the elements. How can i do that? The code I tried is below. Thanks.
我正在尝试将一个变量输入到 ArrayList 中,然后添加所有元素。我怎样才能做到这一点?我试过的代码如下。谢谢。
ArrayList<String> aListNumbers = new ArrayList<String>();
int abc = 23;
aListNumbers.add("abc");
aListNumbers.add("2");
aListNumbers.add("3");
//Java ArrayList Sum All Elements
int sum = 0;
for(int i=0; i < aListNumbers.size(); i++){
sum = sum + Integer.parseInt(aListNumbers.get(i));
}
System.out.println("Sum of all elements of ArrayList is " + sum);
回答by Owen
aListNumbers.add("abc");
aListNumbers.add("abc");
Here you aren't adding the contents of the variable named abc
to the list. You're adding the String "abc" to the list. This will cause a NumberFormatException
when the code tries to parse the character string "abc" into a number - because "abc" just isn't a number.
在这里,您没有将命名的变量的内容添加abc
到列表中。您将字符串“abc”添加到列表中。NumberFormatException
当代码尝试将字符串“abc”解析为数字时,这将导致 a - 因为“abc”不是数字。
aListNumbers.add(abc);
aListNumbers.add(abc);
That's closer to what you want, but it will still complain because the variable abc
isn't a String. Since aListNumbers
expects Strings (as it is an ArrayList<String>
), trying to add anything else will upset the compiler.
这更接近你想要的,但它仍然会抱怨,因为变量abc
不是字符串。由于aListNumbers
需要字符串(因为它是ArrayList<String>
),因此尝试添加其他任何内容都会使编译器感到不安。
aListNumbers.add(Integer.toString(abc));
aListNumbers.add(Integer.toString(abc));
Will work.
将工作。
回答by Sylvain Cloutier
Use an ArrayList<Integer> instead of String?
使用 ArrayList<Integer> 而不是 String?
回答by TJunkie
The OP does not know how to code in java. The error is obvious and is not worthy of this site.
OP 不知道如何在 java 中编码。错误很明显,不值得这个网站。
BUT!
但!
If you spend the time to type anything - I would rather give an answer. Even though its been a couple of decades since I was in those shoes - I remember very well when a simplest thing makes one stuck for hours if not days - I was fortunate to be coding in the same room with really patient (not just knowledgeable) people. So I am for giving the guy some slack.
如果你花时间输入任何东西 - 我宁愿给出一个答案。尽管我已经有几十年没有穿上那双鞋了——我记得很清楚,当一件最简单的事情让一个人卡住几个小时甚至几天——我很幸运能在同一个房间里和非常有耐心的人(不仅仅是知识渊博)一起编码人们。所以我是为了让这家伙松懈下来。
and the answer is - definitely use ArrayList<Integer>
答案是 - 绝对使用 ArrayList<Integer>
but also doing add - don't add("2")
- this is adding a string, instead add(2)
which adds an integer
但也做添加 - 不要add("2")
- 这是添加一个字符串,而不是add(2)
添加一个整数
and if you reallywant to parse those strings then you should have String abc="23";
如果你真的想解析这些字符串,那么你应该有String abc="23";
the more valuable bit of info is- find examples - the web is full of them - get your questions answered faster and avoid disdain.
更有价值的信息是- 查找示例 - 网络上到处都是它们 - 更快地回答您的问题并避免蔑视。
peace
和平