简体   繁体   English

在文件中查找一个单词,将其存储在数组列表中,并确保该单词不会被多次占用?

[英]Finding a word in file storing it in an array list and making sure that word isn't accounted for more than once?

public ArrayList<String> getWords()
{
    int size1 = lines.size();
    int size2 = 0;
    int counter3 = 0;
    ArrayList<Integer> checkthewords;
    for (int x = 0; x < size1; x++)
    {
        size2 = lines.get(x).substring(x).length();
        for (int y = 0; y < size2; y++)
        {
            if (Character.isLetter(charAt(((lines.get(x)).indexOf(x, z + x)))))
            {
                words.set(z, lines.get(x).substring(x,z + 1));
            }
            else
            {
                checkthewords.set(counter3, words);
                counter3++;
            }
            if (checkthewords.get(x).equals(checkthewords.get(counter3)))
            {

            }
        }
    }
    return words;
}

The method above is a called getWords(). 上面的方法叫做getWords()。 I am trying to get a word from a file and store it in the arrayList checkthewords. 我试图从文件中获取一个单词并将其存储在arrayList中检查单词。 I want to make sure that a word is not going to be stored in the arrayList checkthewords more than once. 我想确保一个单词不会被存储在arrayList中多次检查单词。

I have the if statement: 我有if声明:

            if (Character.isLetter(charAt(((lines.get(x)).indexOf(x, z + x)))))

But, don't know where to go from there. 但是,不知道从那里去哪里。

You should use Set in Java to store elements when duplicates are not allowed. 当不允许重复时,您应该使用Set in Java来存储元素。

A collection that contains no duplicate elements. 不包含重复元素的集合。

If you want to retain insertion order as well then use LinkedHashSet which is Hash table and linked list implementation of the Set interface, with predictable iteration order. 如果你想保留插入顺序 ,那么使用LinkedHashSet ,它是Hash表和Set接口的链表实现,具有可预测的迭代顺序。

Kindly refer to below tutorials to understand application of Set in java. 请参考下面的教程来了解Set在java中的应用。
Tutorial 1 教程1
Tutorial 2 教程2
Tutorial 3 教程3

See Also-: 也可以看看-:
HashSet vs TreeSet vs LinkedHashSet HashSet vs TreeSet vs LinkedHashSet
HashSet vs LinkedHashSet HashSet与LinkedHashSet

I'm pretty sure your code won't run at the moment. 我很确定你的代码目前不会运行。 You are doing some strange things in there and I don't really understand it. 你在那里做了一些奇怪的事情,我真的不明白。

Try to approach this one step at a time. 尝试一次接近这一步。

The first step is to get the word from the file. 第一步是从文件中获取单词。 Make sure you can parse the line and extract the word you want. 确保您可以解析该line并提取所需的单词。

Then you need to check if the word exists in your checkthewords list. 然后,你需要检查,如果在你的存在的话checkthewords列表。 If it doesn't exist, add it. 如果它不存在,请添加它。 You can use the contains method provided by List to see if the list contains something. 您可以使用List提供的contains方法来查看列表是否包含某些内容。

if(!checkthewords.contains(word)) {
    // it's not in the list yet, add it
    checkthewords.add(word);
}

Also when you create your checkthewords list, you don't initialise it (so it's null): 此外,当您创建checkthewords列表时,您不会初始化它(因此它为null):

ArrayList<String> checkthewords;

should be: 应该:

ArrayList<String> checkthewords = new ArrayList<String>();

And you shouldn't use checkthewords.set() like that. 你不应该像那样使用checkthewords.set() set is used to replace an existing element, not to add a new element. set用于替换现有元素,而不是添加新元素。 You could easily be setting an element that doesn't exist yet and throw an ArrayIndexOutOfBoundsException . 您可以轻松地设置一个尚不存在的元素并抛出ArrayIndexOutOfBoundsException Use checkthewords.add(word) to add something to your list. 使用checkthewords.add(word)将一些内容添加到列表中。

See the ArrayList documentation . 请参阅ArrayList文档

set(int index, E element)

Replaces the element at the specified position in this list with the specified element. 用指定的元素替换此列表中指定位置的元素。

It seems like you're overthinking this. 看起来你正在过度思考这个问题。 Keep it simple. 把事情简单化。 :) :)

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

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