简体   繁体   English

检查单词是否等于ArrayList中的特定单词

[英]Checking if a word is equal to a specific word in an ArrayList

I'm trying to create a method which will search through an ArrayList containing several words searching for a specific String. 我正在尝试创建一种方法,该方法将搜索包含几个单词的ArrayList,以搜索特定的String。 Where if the String is not equal to any words in the ArrayList it will add the word to the list, and if the word already exists in the list, it will count how many times the word occurs and then add one more, which will represent the last String input. 如果String不等于ArrayList中的任何单词,它将在列表中添加该单词,如果该单词已经存在于列表中,它将计算该单词出现的次数,然后再添加一个,表示最后的String输入。 This is what I've got so far in my code: 到目前为止,这是我在代码中得到的:

public void leggTilOrd(String ord) {
    if (Ord.contains(ord)) {
        teller++;
    }

    if (!Ord.contains(ord)) {
        Ord.add(ord);
    }

    System.out.println(teller);
}

Obviously this will only add one more number in the counter (teller), so what I'm trying to achieve it to add 1 on top of all the occurrences of that specific String in the list and this is where I'm stuck. 显然,这只会在计数器(交易员)中再加上一个数字,因此我要实现的目的是在列表中该特定String的所有出现次数的基础上再加1,这就是我要坚持的地方。

Edit: I should also mention that Ord is an ArrayList I've created earlier in the code. 编辑:我还应该提到Ord是我在代码前面创建的ArrayList。 Here's the full code: 这是完整的代码:

import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;

public class Ordliste {

private ArrayList<String> Ord = new ArrayList<String>();
private int teller = 0;


public void lesBok (String filnavn) throws Exception{
Scanner fil = new Scanner(new File(filnavn));
while(fil.hasNextLine()){
  Ord.add(fil.next());
} fil.close();
}

 public void leggTilOrd(String ord){
 if(Ord.contains(ord)){
  teller++;
  } if (!Ord.contains(ord)){
  Ord.add(ord);
  } System.out.println(teller);
  }
  }

Are you tied to using an ArrayList ? 您是否绑定到使用ArrayList I'd recommend using a Map<String, Integer> instead and store the number of occurrences of a specific string as value in the map: 我建议改用Map<String, Integer>并将特定字符串出现的次数存储为映射中的值:

if (map.get(ord) != null) {
  map.put(ord, map.get(ord) + 1);
}
else {
  map.put(ord, 1);
}

Instead of doing this , you can keep track of the strings in your list using a Set 无需执行此操作,您可以使用Set跟踪列表中的字符串

Set<String> set = new HashSet<String>();

public void leggTilOrd(String ord) {
    if (set.contains(ord) != null) {
        teller++;
    } else {
        Ord.add(ord);
        set.add(ord);
    }

    System.out.println(teller);
}

Still not sure exactly what you are trying to accomplish, do you increment teller each time you add a different word? 仍然不确定您要完成的工作,每次添加不同的单词时您是否增加teller

This is a simple way to do what I believe you are attempting, and should get you going in the right direction. 这是完成我认为您正在尝试的事情的简单方法,应该可以使您朝正确的方向前进。

import java.util.Arrays;
import java.util.List;

public class test
{
  static int teller = 0;

  static List<String> words = Arrays.asList("dog", "cat", "dog");

  public static void main(String[] args)
  {
    System.out.println(teller);
    addAndCount("dog");
    System.out.println(teller);
    teller = 0;
    addAndCount("cat");
    System.out.println(teller);
    teller = 0;
    addAndCount("fish");
    System.out.println(teller);
  }

  public static void addAndCount(String newWord)
  {
    teller += 1 + Math.toIntExact(
        words.stream().filter(string -> newWord.equals(string)).count());
    words.add(newWord);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM