看代码,一个在统计过程中打印每一个值,一个不打印,造成的结果就是后者比前者出现1的次数更多。
单独测试也是如此,即只使用一个函数测试,一样会出现这样的结果package test;import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class TenPercentRandomGame {
private ArrayList list;

public TenPercentRandomGame(){
list = new ArrayList();
}

public void add(String i){
list.add(i);
}
//这个货统值计超过0.1的很少
public void statisticsWithPrint(){
Iterator<String> it = list.iterator();
int account = 0;
while(it.hasNext()){
System.out.println(it.next());
if(it.next().equals("1")){
account++;
}
}
System.out.print("------------------------");
System.out.println((double)account/list.size());
}
//这个货统值计超过0.1的很多,能达到一半以上
public void statisticsWithoutPrint(){
Iterator<String> it = list.iterator();
int account = 0;
while(it.hasNext()){
if(it.next().equals("1")){
account++;
}
}
System.out.print("------------------------");
System.out.println((double)account/list.size());
}

public void fillArray(int total){
for(int i = 0; i < total; i++){
list.add(String.valueOf(new Random().nextInt(9)));
}
}

public static void main(String[] args){
TenPercentRandomGame trg = new TenPercentRandomGame();

for(int i = 0; i < 10; i++){
trg.fillArray(100);
trg.statisticsWithPrint();
trg.list.clear();
}
System.out.println("=========Line Break===========");
for(int i = 0; i < 10; i++){
trg.fillArray(100);
trg.statisticsWithoutPrint();
trg.list.clear();
}

}}
输出值5
7
0
8
6
7
0
3
1
4
5
0
8
5
5
6
3
1
8
1
7
7
6
4
2
5
2
3
8
2
3
3
5
7
1
3
2
5
5
6
2
3
8
5
7
1
2
5
3
2
------------------------0.06
3
6
3
8
8
1
1
5
8
2
2
6
4
3
1
2
1
8
2
0
8
5
0
5
6
4
6
4
6
2
2
5
3
5
6
1
2
3
4
8
8
3
2
5
2
1
2
3
4
2
------------------------0.07
6
8
2
7
3
3
5
3
8
3
3
3
7
8
3
7
5
4
4
7
7
2
1
4
0
7
1
2
3
8
5
6
6
7
0
6
8
0
0
1
8
7
7
2
8
0
3
6
3
2
------------------------0.08
8
4
3
4
2
1
1
1
6
4
1
1
8
5
2
4
5
3
1
5
0
8
2
4
1
6
2
5
3
0
8
1
6
8
4
1
3
8
8
3
2
0
3
5
3
6
6
4
4
8
------------------------0.06
7
3
2
2
3
7
3
8
5
4
4
7
8
6
3
0
3
6
7
3
6
4
7
5
3
1
0
4
5
0
2
3
1
5
6
2
5
0
0
8
0
0
3
4
5
5
5
0
7
0
------------------------0.03
7
3
0
0
7
2
4
7
0
6
4
1
1
1
4
2
0
6
8
8
4
0
5
1
1
4
0
6
7
7
2
6
0
8
2
4
7
2
0
1
3
4
2
0
1
3
8
3
8
6
------------------------0.04
4
0
0
0
7
6
4
2
7
5
2
1
0
1
1
3
3
6
2
7
1
7
6
4
6
4
5
0
3
1
4
4
7
5
5
1
3
3
8
1
1
1
4
8
1
4
5
1
5
6
------------------------0.1
6
8
7
3
7
1
8
1
2
1
1
8
8
3
5
7
4
0
4
1
7
7
7
6
1
0
5
4
4
7
5
4
8
6
4
6
6
2
7
2
1
5
4
8
4
7
3
2
0
0
------------------------0.07
5
8
1
7
8
7
2
2
1
8
1
6
2
8
2
3
0
8
5
2
1
7
0
0
5
0
4
3
1
2
3
6
7
1
0
4
7
7
6
4
4
5
1
0
6
3
1
1
4
0
------------------------0.06
2
5
4
3
0
7
2
1
8
0
0
3
0
5
3
6
7
5
1
4
8
2
6
8
4
4
7
3
0
5
5
3
1
8
3
7
2
0
6
3
0
0
1
0
4
1
3
6
8
7
------------------------0.14
=========Line Break===========
------------------------0.12
------------------------0.11
------------------------0.09
------------------------0.08
------------------------0.17
------------------------0.07
------------------------0.12
------------------------0.15
------------------------0.09
------------------------0.12

解决方案 »

  1.   


    //这个货统值计超过0.1的很少
        public void statisticsWithPrint(){
            Iterator<String> it = list.iterator();
            int account = 0;
            while(it.hasNext()){
                System.out.println(it.next());
                if(it.next().equals("1")){
                    account++;
                }
            }
            System.out.print("------------------------");
            System.out.println((double)account/list.size());
        }这段代码有问题
    你调用了2次it.next...两次调用,他已经往下移动了,指向的不是同一个值。
    你必须设一个中间变量String temp = it.next();
    再用temp去操作
      

  2.   

                System.out.println(it.next());这句去掉
    会浪费一次循环的
      

  3.   

    可以定义一个临时变量保存一下it.next嘛,然后在操作,,
      

  4.   

    原来如此,对iterator理解的不够。谢谢两位啦