2014-03-26 2 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(); 

    } 
} 

여기에 코드를 단순화하고 프로그래밍 어떤 당신이 HashSet, LinkedHashSet 또는 TreeSet 대신 ArrayList의 사용할 수있는 중복 제거 할 필요는 없습니다하기 위해

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

'List '대신'Set '을 사용하지 않는 이유는 무엇입니까? 이 방법은 중복 제거가 자동으로 수행됩니다 –

+0

새 이름을 추가 할 때마다 전체 이름 목록을 실제로 인쇄 하시겠습니까? 또한이 방법을 사용하려면 모든 항목을 추가 한 후에 중복 항목을 한 번만 제거하면됩니다. 현재 가능한 복제본이 하나 뿐인 경우 가능한 모든 쌍을 비교하고 있습니다. – Holloway

답변

0

Set 사용에 대한 모든 사용자의 의견이 올 바릅니다. 이것은 사용해야하는 데이터 구조입니다. 그러나 코드의 문제는 find() 방법에 있습니다. int i을 전달하고 String s = names.get(i)을 설정하면 중첩 for 루프가 실행되지만 문자열 s는 변경되지 않습니다.

이 시도 : 당신은 루프에 대한 귀하의 내부의 i 번째 요소에 동일의 설정

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; 
} 

공지 사항. 당신은 당신의 방법에서 매개 변수 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

내 공공 주 이외의 방법을합니다.

기본적으로 하나를 :

  • 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 모두 폐기 할 수있다.

두 경우 모두 복제본은 대소 문자를 구분합니다.하지만 지금은 코드가하는 일입니다.