如何验证数组列表的字符串值的升序?

时间:2014-01-10 17:29:28

标签: java selenium-webdriver

我正在使用带有Java绑定的Selenium Webdriver,我正在测试一个排序功能,您可以在arraylist = {"4"","4.5"","5.5""}中排列值。所以基本上字符串包含小数点和双引号。我在下面有以下代码。问题是我不断变得虚假,因为当它将当前与之前的电流进行比较时,它会出现错误。谢谢你的帮助

public Boolean checkAscendingOrderScreensize(List<String> list){
   if(list == null || list.isEmpty())
        return false;
    if(list.size() == 1)  
        return true;
    for(int i=1; i<list.size();i++)
    {
        String current = list.get(i).toString();
        String previous = list.get(i-1).toString();
        current = current.replace(",",".");
        current = current.replace("\"", "");
        previous = previous.replace(",",".");
        previous = previous.replace("\"", "");

        if(current.compareTo(previous)>0)
                return false;
    }
    return true; 
}

1 个答案:

答案 0 :(得分:0)

转换前需要将字符串转换为double。所以解决方法将是

 public class Main {

    public static void main(String bicycle[]) {
        List<String> texts = new ArrayList<String>();
        texts.add("4\"");
        texts.add("4.5\"");
        texts.add("5.5\"");
        System.out.println(checkAscendingOrderScreensize(texts));
          // prints true

    }

    public static boolean checkAscendingOrderScreensize(List<String> list) {

        if (list == null || list.isEmpty())
            return false;

        if (list.size() == 1)
            return true;

        for (int i = 1; i < list.size(); i++) {
            String current = list.get(i).toString();
            String previous = list.get(i - 1).toString();
            current = current.replace(",", ".");
            current = current.replace("\"", "");
            previous = previous.replace(",", ".");
            previous = previous.replace("\"", "");

            if(Double.valueOf(current)<Double.valueOf(previous))


                return false;
        }

        return true;
    }

}