2013-09-21 7 views
0

내가 학교에 대한 할당을 반환 TreeSet의에 추가 할 때이 내가 지금까지 내가 별도의 파일을는 내가 그것을 빈

import java.io.*; 
import java.util.Scanner; 

public class UniquesDupesTester 
{ 
    public static void main(String args[]) throws IOException 
    { 
     // make a Scanner and associate it with "UniquesDupes.dat" 
     // as long as there are Strings in the file 

      // read in a String, 
      // create a UniquesDupes object with it 
      // print the object 

      Scanner in = new Scanner(new File("UniquesDupes.dat")); 



      while (in.hasNextLine()) 
      { 
       String n = in.nextLine(); 
       UniquesDupes a = new UniquesDupes(n); 
       a.getUniques(); 
       a.getDupes(); 
       System.out.println (a); 
      } 




    } 
} 

할 노력하고있어의 메모와 함께이 무엇

import java.util.Set; 
import java.util.TreeSet; 
import java.util.Arrays; 
import java.util.ArrayList; 

public class UniquesDupes 
{ 
    private ArrayList<String> list; 


    /** 
    * constructs a UniquesDupes object such that list contains the space delimited strings 
    * parsed from input 
    * @param input a String containing the list of words separated by spaces 
    */ 
    public UniquesDupes(String input) 
    { 
     list = new ArrayList<String>(); 

     String[] words = "abc cde fgh ijk".split(" "); 
     ArrayList<String> list = new ArrayList<String>(Arrays.asList(words)); 
    } 

    /** 
    * returns a set of Strings containing each unique entry in the list 
    */ 
    public Set<String> getUniques() 
    { 
     Set<String> uniques = new TreeSet<String>(); 

     for(String a:list) 
     { 
      uniques.add(a); 
     } 

     return uniques; 
    } 

    /** 
    * returns a set of Strings containing each entry in the list that occurs more than once 
    */ 
    public Set<String> getDupes() 
    { 
     Set<String> uniques = new TreeSet<String>(); 
     Set<String> dupes = new TreeSet<String>(); 

     for(String a:list) 
     { 
      uniques.add(a); 
     { 
      if(uniques.add(a) == false) 
      { 
       dupes.add(a); 
      } 
     } 
     } 


     return dupes; 
    } 

    /** 
    * returns the original list, the list of duplicates and the list of uniques 
    * @return the String version of the object 
    */ 
    public String toString() 
    { 
     return "Orig list :: " + list 
       + "\nDuplicates :: " + getDupes() 
       + "\nUniques :: " + getUniques() + "\n\n"; 
    } 
} 
여기

당신이 그것을 컴파일 및 실행되지만 모든 파일이 내가 아무 생각 w이없는 빈 반환

a b c d e f g h a b c d e f g h i j k 
one two three one two three six seven one two 
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6 

필요하면 DAT 파일입니다 모자 나는 틀린 도움 또는 힌트가 appreicated 될 것입니다

답변

1

논리는 거의 맞습니다.

UniquesDupes 클래스

거의 OK이지만, 생성자는 확인하지 않습니다. 루프가 조금 변경해야합니다 동안

public void addString(String input) { 
    String[] words = input.trim().split(" "); //split the input string into words by spaces, after trimming whitespace 
    this.list.addAll(Arrays.asList(words)); //adding them to the list. 
} 

: 그것은 단지 동시에

public UniquesDupes() // no args, default constructor 
{ 
    list = new ArrayList<String>(); //just initialize the list 
} 

해야합니다, 클래스는 addString 방법을해야했다. UniqueDupes 클래스의 인스턴스를 1 개만 원하는 경우 이전에 만든 addString 메서드를 사용하여 각 행을 추가합니다.

 UniquesDupes a = new UniquesDupes(); //create it here 
     while (in.hasNextLine()) 
     { 
      String n = in.nextLine(); 
      a.addString(n); //adding the string 
     } 

그런 다음 결과는 논리를 잘 거의했다

  Collection<String> uniques = a.getUniques(); 
      Collection<String> dupes = a.getDupes(); 

      System.out.println (uniques.toString()); 
      System.out.println (dupes.toString()); 

... 당신은 않았던 추한 것은 그러나이 부분

입니다

다르게 처리해야 :

list = new ArrayList<String>(); //using instance variable 

    String[] words = "abc cde fgh ijk".split(" "); 
    ArrayList<String> list = new ArrayList<String>(Arrays.asList(words)); 
    // ^^ declaring local variable the same name as the instance variable 

이것은 입니다.입니다. 당신은 그것을해서는 안됩니다. 아니 아니. 다시하지 마! 이 습관을 집어내는 것은 코드를 예외적으로 읽기가 어렵게 만들고 미친 미친놈을 유지하는 것입니다 ...