【问题标题】:Check if strings on an arraylist are the same? [duplicate]检查arraylist上的字符串是否相同? [复制]
【发布时间】:2015-04-02 04:59:37
【问题描述】:

CandidateList 是一个包含候选人姓名的列表。 ballot 是一个字符串列表。我想检查选票名单上的名字是否与候选人名单上的任何名字相同。如果不是,那么我想检查选票上的下一个字符串,看看该字符串是否与候选列表中的任何名称相同。等等……

/**
     * @param candidateList a list of candidate names
     * @return the name of the first choice candidate for this Ballot from those
     * in candidateList
     */
    public String firstChoiceFrom(ArrayList<String> candidateList) {

        String firstChoice = "";
        String candidate = "";
        for (int can = 0; can < candidateList.size(); can++) {

            String bal = ballot.get(can); // gets next ballot
            candidate = candidateList.get(can); // gets next candidate
             while (!candidate.equals(bal))
             {

             }
            //checks to see whether the first name on the ballot
            if (candidate.equals(bal)) {        
                return candidate;
            } 

        }

【问题讨论】:

  • 你能有更好的格式,更清楚你真正想要什么吗?
  • 我有代码,我只是觉得它的路要走。
  • @Name 我不认为没有人以此来评判你。我们是来帮忙的。我觉得如果你能有一个更好的语法并发布你对特定问题所做的事情,你会得到更好的结果
  • 如果Set 满足您的要求并且您真的想要list1 ∩ list2,请查看docs.oracle.com/javase/7/docs/api/java/util/…
  • 我添加了我的代码。如果有人知道我能做什么,那将有所帮助。

标签: java


【解决方案1】:

如果找到匹配项,您想做什么?

for(String s : list1){
if(list2.contains(s)){
// set a flag and break
}
}

这个块基本上迭代列表一,检查字符串是否包含在列表二中。代码可以根据你想要实现的目标进行改进。

按要求编辑

List<String> list1 = new ArrayList<String>();

    list1.add("1");
    list1.add("2");
    list1.add("3");

    List<String> list2= new ArrayList<String>();
    list2.add("11");
    list2.add("12");
    list2.add("3");

    List<String> list3 = new ArrayList<String>(list2);
    list3.retainAll(list1);
    System.out.println(list3);
    //if you dont mind replacing your initial list, then you can use
    list1.retainAll(list2);
    System.out.println(list1);

【讨论】:

  • 感谢您的回复。我要做的就是返回两个列表都有的字符串。
【解决方案2】:

如果您想比较两个列表并只保留常见的列表。就像你在你的一个 cmets 中提到的那样

我要做的就是返回两个列表都有的字符串。

你可以在下面做:

list1.retainAll(list2); // This will keep all elements that matches list 2 and remove other
for(String word: list1)
{
System.out.println(word); //it will print all words in list 1
}

编辑:回复您的评论。是的,你可以一个一个地做,但不推荐,因为列表有retainAll的功能

一一比较是这样的:

for(String word : list1) //for each string in list 1
{
    if list2.contains(word);  // if list2 has the string
    {
      list3.add(word);  // add it in new list
    }
}

然后你可以打印如上所示的列表 3

【讨论】:

  • 谢谢你,这很有帮助。有没有办法检查 list1 上的第一个字符串,看看它是否与 list2 上的任何字符串相同。如果不是,则检查 list1 上的下一个字符串,看看它是否与 list2 上的任何字符串相同。等等。
  • @Name 本质上就是一个交叉点 - 在这张图片中设想 A == list1B == list2 upload.wikimedia.org/wikipedia/commons/d/da/… - 之后的结果列表是 list1 ∩ list2
  • @Name 更新了答案。这是漫长的道路和额外的工作。始终使用提供的功能,因为它们本质上做相同的工作。
  • 谢谢你,这正是我所需要的。
  • @Name 不客气 :)
【解决方案3】:

您想要处理的方式就是您描述它的方式:对于您投票箱中的每张选票,如果选票与候选人的姓名匹配,那么我们希望将其退回。

如果我们没有找到任何东西,请返回 null。实际上,我们无能为力。请注意,这不包括重复(即投票箱中有两个候选人的姓名相同),尽管可以进行简单的修改以使其涵盖这种情况。

for(String ballot : ballotBox) {
    if (candidateList.contains(ballot)) {
        return ballot;
    }
}
return null;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 2018-12-16
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多