2013-09-21 35 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"; 
    } 
} 

這裏是dat文件,如果你需要它

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 

它編譯和運行,但所有的文件返回空白我不知道w ^帽子我做了錯誤的幫助或暗示

回答

1

的邏輯是差不多正確。

UniquesDupes類幾乎沒問題,但構造函數並不正確。它應該只是

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

同時表示,類將需要一個addString方法:

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

while循環應該修改一下。您只需要UniquesDupes類的一個實例,然後使用之前創建的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 

這是不好的。你不應該這樣做。不,不。不要再這樣做!撿起這種習慣使代碼格外難以閱讀,瘋狂的瘋狂維護...

相關問題