2014-03-26 29 views
2

我的程序讀取輸入文件,這是一個txt file,其中包含名字和姓氏的重複。我不確定爲什麼removeDuplicate方法不刪除重複項,而是給我一個錯誤。我究竟做錯了什麼?我無法讓我的removeDuplicate方法工作?

public static void main(String[] args) throws IOException { 

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

     String fName; 
     String lName; 

     System.out.println("What is the input file?"); 

     Scanner kb = new Scanner(System.in); 
     String fileName = kb.next(); 

     File list = new File(fileName); 

     Scanner in = new Scanner(list); 

     System.out.println("What is the output file?"); 

     String outFileName = kb.next(); 

     PrintWriter outFile = new PrintWriter(outFileName); 

     while (in.hasNext()) { 

      fName = in.next(); 

      lName = in.next(); 

      names.add(fName + " " + lName); 
      removeDuplicates(names); 
      display(names); 


      outFile.println(fName + " " + lName); 

     } 
     outFile.close(); 

    } 
} 

這裏的方法我的公共主

public class StudentList { 

    public static void display(ArrayList<String> n) { 
     // step through all positions of the ArrayList n and display the values 
     // at each positoin 
     for (int i = 0; i < n.size(); i = i + 1) { 
      System.out.println(n.get(i)); 
     } 
    } 


    public static int find(ArrayList<String> names, int i) { 

     String s = names.get(i); 
     for (i = 0; i < names.size(); i = i + 1) { 
      for (int j = i + 1; j < names.size(); j = j + 1) { 
       if (s.equals(names.get(j))) { 

        return j; 

       } 
      } 

     } 
     return -1; 
    } 


    public static void removeDuplicates(ArrayList<String> names) { 

     for (int i = 0; i < names.size(); i = i + 1) { 
      while (find(names, i) > 0) { 
       names.remove(find(names, i)); 
      } 
     } 

    } 
+1

你得到什麼錯誤? –

+5

爲什麼不使用'Set '而不是'List '?這種方式重複被自動刪除 –

+0

每次添加新的名稱時,是否真的要打印出整個名稱列表?另外,如果您想使用此方法,則只需在添加完所有內容後刪除一次重複項。此刻,當只有一個可能的重複時,您會比較每對可能的對。 – Holloway

回答

0

大家對使用Set的評論是正確的。這是您應該使用的數據結構。但是,您的代碼問題出在您的find()方法中。你傳遞一個int i並設置String s = names.get(i)然後你執行一個嵌套for循環,但不會改變你的字符串。

試試這個:

public static int find(ArrayList<String> names) { 


    for (i = 0; i < names.size(); i = i + 1) { 
     String s = names.get(i); 
     for (int j = i + 1; j < names.size(); j = j + 1) { 
      if (s.equals(names.get(j))) { 

       return j; 

      } 
     } 

    } 
    return -1; 
} 

請注意,您的for循環集合S等於第i個元素的內部。您不再需要方法中的參數I.但是,這可能會改變你的代碼。如果你想簡單地嘗試找到每次發生的事情,你會想這:

public ArrayList<Integer> find(String name, ArrayList<String> names) { 
    ArrayList<Integer> duplicateIndices = new ArrayList<Integer>(); 
    for (int i = 0; i < names.size(); i++) { 
    if (names.get(i).equals(name)) { 
     duplicateIndices.add(new Integer(i)); 
    } 
    } 
    return duplicatIndices; 
} 
3

之外爲了簡化你的代碼,而不是需要以編程方式刪除任何重複的,你可以使用的ArrayList而不是HashSetLinkedHashSetTreeSet

基本上之一:

  • Set<String> names = new HashSet<String>(); // unordered, doesn't keep duplicates
  • Set<String> names = new LinkedHashSet<String>(); // keeps insertion order, doesn't keep duplicates
  • Set<String> names = new TreeSet<String>(); // ordered by lexicographic order, doesn't keep duplicates

然後,您可以配置兩個findremoveDuplicates的。

請注意,在任何情況下,重複項都是區分大小寫的 - 但這就是您的代碼目前所做的。