java 使用java在列表中添加数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16623154/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:30:27  来源:igfitidea点击:

Adding Number in a list using java

java

提问by Koneri

I have written the following code to add the elements of the java. but i am unable to continue from one part here.

我编写了以下代码来添加 java.util.Date 的元素。但我无法从这里的一部分继续。

enter code here
import java.io.*;
import java.util.*;
class adder
{ 
public static void main(String[] args)
{
 List<Integer> list = new ArrayList<Integer>();
 int b;
 System.out.println("Enter the number");
 InputStreamReader inp = new InputStreamReader(System.in);
 BufferedReader bnp = new BufferedReader(inp);
 list.add(Integer.parseInt(bnp.readLine()));
  for(int i=0;i<list.size();i++)
  {
  b += list[i];
  }
   System.out.println("The answer is" + b);
 }
  }

 **OUTPUT**.
  add.java:15: array required, but java.util.ArrayList<java.lang.Integer> found
  b += list[i];

回答by Benjamin Gruenbaum

Java doesn't let you use array indexers on ArrayLists

Java 不允许您在 ArrayLists 上使用数组索引器

 b += list[i];

Is illegal. Should be

是非法的。应该

 b+= list.get(i);

See the ArrayList API

请参阅ArrayList API

回答by Julien Bodin

This is because you are trying to access your List content as if it was an array (with list[i]). The proper way is to use : list.get(i).

这是因为您试图访问您的 List 内容,就好像它是一个数组(使用 list[i])。正确的方法是使用:list.get(i)。

回答by PSR

You need to use .get()method in list to retrieve the elements from list

您需要使用.get()列表中的方法从列表中检索元素

replace

代替

   b += list[i];

with

 b+= list.get(i);

回答by Aniket Thakur

One major mistake that I see is that you have not initialized bvariable. This is a local variable and must always be initialized before using. You code will not even compile as compiler won't allow this.

我看到的一个主要错误是您没有初始化b变量。这是一个局部变量,在使用之前必须始终进行初始化。您的代码甚至不会编译,因为编译器不允许这样做。

Another mistake is you add only one value to the List/Array(what ever you prefer) and then you try to iterate over the values stores. Makes no sense to me.

另一个错误是您只向列表/数组添加一个值(无论您喜欢什么),然后尝试遍历值存储。对我来说毫无意义。

Next about your question, you seem to be confused between arrays and List. Either store your data in an array(in which case your for loop logic is correct) or else if you wish to use List then iterate over it to get your desired result.You can do something like the following -

接下来关于您的问题,您似乎对数组和列表感到困惑。要么将您的数据存储在一个数组中(在这种情况下您的 for 循环逻辑是正确的),或者如果您希望使用 List 然后对其进行迭代以获得您想要的结果。您可以执行以下操作 -

int b = 0;
for(Integer number : list)
{
   b = b + number;
}
System.out.println("The answer is" + b);

Note : Internally this will also create an iterator of list and call .next() to get you all the values in the list. But I feel this is a smarter and better way.

注意:在内部这也将创建一个列表迭代器并调用 .next() 来获取列表中的所有值。但我觉得这是一种更聪明、更好的方法。