【发布时间】:2017-07-01 00:06:28
【问题描述】:
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;
}
上面的方法是一个叫做getWords()的方法。我正在尝试从文件中获取一个单词并将其存储在 arrayList checkthewords 中。我想确保一个单词不会多次存储在 arrayList checkthewords 中。
我有 if 语句:
if (Character.isLetter(charAt(((lines.get(x)).indexOf(x, z + x)))))
但是,不知道从那里去哪里。
【问题讨论】:
-
你能用集合代替列表吗?
-
getWords()应该返回什么?一个独特的单词列表?LinkedHashSet可能是解决这个问题的方法(假设您需要保留插入顺序)。 -
你的意思是普通数组吗?
-
getWords() 应该返回一个包含文件所有单词的数组或数组列表
-
如果这是家庭作业,我认为使用
LinkedHashSet可能不是理想的解决方案,而是需要手动处理重复项。 @J。如果您为变量使用有意义的名称,您是否可以使您的代码更清晰:size1、size2、counter3、x、y- 这些都传达了关于其用途的零信息。
标签: java