Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取ArrayList的元素之和_Java_For Loop_Arraylist - Fatal编程技术网

Java 获取ArrayList的元素之和

Java 获取ArrayList的元素之和,java,for-loop,arraylist,Java,For Loop,Arraylist,我试图得到ArrayList元素的总和 我只能打印数字,但不能打印总数 现在它显示的1,1,1,1,1等于5,这是正确的数字。但是我希望它显示5 ArrayList <history> history = historyDAO.getHistory(x, y); ArrayList <Integer> totalWins = new ArrayList <Integer> (); for (history his : history) { St

我试图得到ArrayList元素的总和

我只能打印数字,但不能打印总数

现在它显示的
1,1,1,1,1
等于5,这是正确的数字。但是我希望它显示
5

ArrayList <history> history = historyDAO.getHistory(x, y);
ArrayList <Integer> totalWins = new ArrayList <Integer> ();

for (history his : history) {
       String res = his.getRes();
             if (res.equals("Vundet")) {
                    int totalW = 0; 
                    totalW++;
                    totalWins.add(totalW);
              System.out.print(totalWins);
}}
arraylisthistory=historyDAO.getHistory(x,y);
ArrayList totalWins=新的ArrayList();
历史(他的历史){
String res=his.getRes();
如果(res.equals(“Vundet”)){
int totalW=0;
totalW++;
totalWins.add(totalW);
系统输出打印(totalWins);
}}

为了更好的测量,我将提到
ArrayList
引用了一个存储所有数据的类。现在我只想处理
res
值等于
Vundet

的对象,似乎您正在尝试计数“Vundet”条目。这可以通过递增计数器变量来实现:

int count = 0;
for (history his : history) {
  if (his.getRes().equals("Vundet")) {
    count++;
  }
}
System.out.println(count);
您也可以使用以下方法编写:


要获得问题状态下元素的
,可以使用streams

int sum = history.stream()
    .filter(his->his.getRes().equals("Vundet"))
    .mapToInt(his->his.getWins()) // assuming this getter exists.
    .sum();

好的,问题是: totalW是一个用来计算发生率的变量,在这里之前你都很好。唯一的问题是这个变量的声明,使它0应该在上面,否则它就像计数1一样,然后重置为0,然后再次计数1,而不是变为2

另一个问题是,您希望在最后打印此变量的值,而不是每次计数时打印,因此这里是最后一个:

ArrayList <history> history = historyDAO.getHistory(x, y);
ArrayList <Integer> totalWins = new ArrayList <Integer> ();

for (history his : history) {
   String res = his.getRes();
         int totalW = 0; 
         if (res.equals("Vundet")) {

                totalW++;
                totalWins.add(totalW);

}System.out.print(totalWins);
}
arraylisthistory=historyDAO.getHistory(x,y);
ArrayList totalWins=新的ArrayList();
历史(他的历史){
String res=his.getRes();
int totalW=0;
如果(res.equals(“Vundet”)){
totalW++;
totalWins.add(totalW);
}系统输出打印(totalWins);
}

您的代码正在打印
[1,1,1,1,1]
,因为您正在打印
totalWinds
,它是一个
列表

如果要打印发生了多少次
history.getRes().equals(“Vundet”)
退出循环或使用流选项

totalW退出循环

ArrayList <History> history = historyDAO.getHistory(x, y);

int totalW = 0; // Out of the loop so it does not get initialize every iteration
for (history his : history) {
    String res = his.getRes();
    if (res.equals("Vundet")) {
        totalW++;
    }
}
System.out.println(totalW);
arraylisthistory=historyDAO.getHistory(x,y);
int totalW=0;//循环外,所以它不会每次迭代都初始化
历史(他的历史){
String res=his.getRes();
如果(res.equals(“Vundet”)){
totalW++;
}
}
系统输出打印项次(totalW);
或者是溪流

ArrayList <History> history = historyDAO.getHistory(x, y);
long total = history.stream()
                .filter(his -> his.getRes() != null && !his.getRes().isEmpty() && his.getRes().equals("Vundet"))
                .count();
arraylisthistory=historyDAO.getHistory(x,y);
long total=history.stream()
.filter(his->his.getRes()!=null&&!his.getRes().isEmpty()&&his.getRes().equals(“Vundet”))
.count();

系统移出循环。
你的历史“类”是什么样子的?谢谢-但这似乎也不起作用。现在我在System.out.print中得到12345。我想得到等于5的和。上面的代码打印一个值,只有一个调用
System.out.println
在循环之外。您的代码中缺少键入的内容。谢谢!正如你和卡罗尔早些时候指出的,溪流解决了我的问题。正如我所说的,我还是一个新手,所以我对溪流没有太多的经验。这是一种正确的方法吗?这是一种方法。溪流的存在是为了帮助,而不是命令。你也可以用循环来做,这很好。但你的问题中没有足够的信息来准确回答。你到底拿的是多少?
ArrayList <History> history = historyDAO.getHistory(x, y);
long total = history.stream()
                .filter(his -> his.getRes() != null && !his.getRes().isEmpty() && his.getRes().equals("Vundet"))
                .count();