2013-04-01 107 views
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, 

任何帮助将不胜感激!

+2

读http://stackoverflow.com/questions/357421/what-is-the-best-way-to-remove-duplicates-in- an-array-in-java –

+8

将数组转换为一组,然后将其转换回数组。 –

+1

想法:1)对数组进行排序2)对照下一个项目检查每个项目3)如果item [i] == item [i + 1]'删除重复项。 –

回答

4

要读取文件,并删除重复的话:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.StreamTokenizer; 
import java.util.Set; 
import java.util.TreeSet; 

public class WordReader { 

    public static void main(String[] args) throws Exception { 
     BufferedReader br = 
     new BufferedReader(
      new FileReader("F:/docs/Notes/Notes.txt")); 
     Set<String> words = new TreeSet<>();    // {sorted,unique} 
     StreamTokenizer st = new StreamTokenizer(br); 
     while(st.nextToken() != StreamTokenizer.TT_EOF) { 
     if(st.ttype == StreamTokenizer.TT_WORD) { 
      words.add(st.sval); 
     } 
     } 
     System.out.println(words); 
     br.close(); 
    } 
}