1
我正在读取一个文件,然后调用一个字符串[]方法,将该行分成单个单词,将每个单词添加到一个唯一单词数组(无重复单词),以及返回唯一字的数组。如何从java中的数组中删除重复的单词
我不知道如何只打印每个单词一次,但这是我到目前为止。
static public String[ ] sortUnique(String [ ] unique, int count)
{
String temp;
for(int i = 1; i < count; i++) {
temp = unique[i].replaceAll("([a-z]+)[,.?]*", "$1");;
int j;
for(j = i - 1; j>= 0 && (temp.compareToIgnoreCase(unique[j]) < 0);j--) {
unique[j+1] = unique[j];
}
unique[j+1] = temp;
}
return unique;
}
这里是数据文件。
Is this a dagger which I see before me,
The handle toward my hand? Come, let me clutch thee.
I have thee not, and yet I see thee still.
Art thou not, fatal vision, sensible
To feeling as to sight? Or art thou but
A dagger of the mind, a false creation,
任何帮助将不胜感激!
读http://stackoverflow.com/questions/357421/what-is-the-best-way-to-remove-duplicates-in- an-array-in-java –
将数组转换为一组,然后将其转换回数组。 –
想法:1)对数组进行排序2)对照下一个项目检查每个项目3)如果item [i] == item [i + 1]'删除重复项。 –