【问题标题】:Stored the values in list and then sorting them using Collections is not working将值存储在列表中,然后使用 Collections 对它们进行排序不起作用
【发布时间】:2015-10-29 11:58:59
【问题描述】:

我正在尝试使用变量“optionsList”将字符串值存储在列表中。但它抛出了 NullPointer 异常。有人可以帮我解决这个问题吗?

下面是代码:

public static void CheckOptionsPresent(String s1) 抛出异常 { 试试{

           List optionsList = null;

           webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance button")));

           webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance button")).click();

           List<WebElement> list = webDriver.findElements(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li"));

           int listcount = list.size();           
           System.out.println(listcount);
           String options[]=new String[listcount];


           for (int i=3; i<=listcount; i++ )
           {

           options[i-3] = webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li:nth-child("+i+") a span")).getText();
           System.out.println(options[i-3]);
                   webDriver.findElement(By.cssSelector("article.ContactInfo.active div.half-left.contactInfo div.idProvinceOfIssuance div ul li:nth-child("+i+") a span")));

           optionsList = Arrays.asList(options);

           }

           System.out.println(optionsList);

           optionsList.removeAll(Collections.singleton(null));


           Collections.sort(optionsList);


           System.out.println("Sorted List:" + optionsList);

}

【问题讨论】:

  • 你能显示你的代码的输出吗?
  • 输出 : 14 BC - British Columbia MB - Manitoba NB - New Brunswick NL - Newfoundland and Labrador NS - Nova Scotia NT - Northwest Territories NU - Nunavut ON - Ontario PE - Prince Edward Island QC - Québec SK - Saskatchewan YT - Yukon Territory [BC - British Columbia, MB - Manitoba, NB - New Brunswick, NL - Newfoundland and Labrador, NS - Nova Scotia, NT - Northwest Territories, NU - Nunavut, ON - Ontario, PE - 爱德华王子岛, QC - 魁北克, SK - 萨斯喀彻温省, YT - 育空地区, null, null] null
  • Vivek,感谢您的回复。但我是 Java 新手,不知道如何实施您的建议,
  • 为什么它不起作用?您的输出已排序。公元前 14 年......,MB......,NB......,YT......
  • for (int i=3; i

标签: java list sorting collections


【解决方案1】:

这是一个字符串列表。你应该使用Collections.sort(optionsList,String.CASE_INSENSITIVE_ORDER)

【讨论】:

  • Sanik 感谢您的回复,但输出仍然相同。
【解决方案2】:

下面是你上面的简化版本,下面的工作正常。

  public static void main(String[] args) {
        List optionsList = null;
        String options[] = new String[3];// listcount];
        options[0] = "cat";
        options[1] = "dog";
        options[2] = "alpha";
        optionsList = Arrays.asList(options);
        System.out.println(optionsList);
        optionsList.removeAll(Collections.singleton(null));
        Collections.sort(optionsList);
        System.out.println("Sorted List:" + optionsList);

    }

我认为问题在于您读取选项 [] 值的方式。

尝试缩小问题范围,我很确定它会起作用。

【讨论】:

  • 非常感谢 Syed :) 它对我有用。你的回答真的很有帮助。
  • 赛义德,我无法为您的答案投票。它说“一旦您总共获得 15 声望,那么您的投票将改变公开显示的帖子分数”。
  • 酷,没问题 :) 我认为您可以将其标记为答案。
猜你喜欢
  • 2021-06-28
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 2022-11-12
相关资源
最近更新 更多