带有if else comapring的ArrayList

时间:2018-05-16 12:18:23

标签: java arraylist

我正在制作一个包含两个读数的程序,一个读数低于'100',另一个读数超过100个。现在我已经让他们的ArrayList将不同的读数存储在一个。

现在,当我对它们使用if语句时,我收到以下错误:运算符'>'不能应用于'java.util.ArrayList>','double'

我是Java编码的新手,目前正在学习它。请帮助我,指导我如何完成这项任务,或者是否有任何替代解决方案。

public class DataGen implements SourceFunction<String> {
boolean running = true;
@Override
public void run(SourceContext <String> ctx) throws Exception {
    try{
        while (running) {
            double vibReading1 = 65.563456;
            double vibReading2 = 75.234450;
            double vibReading3 = 85.987653;
            double vibReading4 = 95.854524;
            double vibReading5 = 98.854524;


            double vibDefReading = 100.000000;

            ArrayList<Double> vibReadingless100 = new ArrayList <Double> ();
            vibReadingless100.add (vibReading1);
            vibReadingless100.add (vibReading2);
            vibReadingless100.add (vibReading3);
            vibReadingless100.add (vibReading4);
            vibReadingless100.add (vibReading5);

            double viBReading1 = 110.453456;
            double viBReading2 = 120.126543;
            double viBReading3 = 130.827468;
            double viBReading4 = 140.082354;
            double viBReading5 = 150.432356;

            ArrayList<Double> vibReadingabove100 = new ArrayList <Double> ();
            vibReadingabove100.add (viBReading1);
            vibReadingabove100.add (viBReading2);
            vibReadingabove100.add (viBReading3);
            vibReadingabove100.add (viBReading4);
            vibReadingabove100.add (viBReading5);

            for (int i = 0; i<100;i++) {

                ctx.collect ("Vibration normal Reading: " + vibReadingless100);
                Thread.sleep (100);
            }
            for (int j =0;j<100;j++){

                ctx.collect ("Vibration WARNING Reading: " + vibReadingabove100);
                Thread.sleep (100);
            }

            ArrayList <ArrayList <Double>> RegularReading = new ArrayList <> ();
            RegularReading.add (vibReadingless100);
            RegularReading.add (vibReadingabove100);

            for(Double item : RegularReading) {
                if (item > vibDefReading) {
                    ctx.collect (" -- ALERT Vibration Above threshold!: " + item);
                }
            }
            running = false;
        }
    } catch (Exception e){
        e.printStackTrace ();
    }
}

@Override
public void cancel() {
    running = false;
}}

1 个答案:

答案 0 :(得分:0)

您无法将列表与数字进行比较。这就是消息所说的内容。

如果要查明列表中的任何值是否高于给定值,请使用循环:

for (Double item : rr) {
    if (item > vibDefReading) {
        ctx.collect (" -- ALERT Vibration Above threshold!: " + item);
    }
}
如果您想在第一次高读时停止,请使用可选break