2011-12-02 79 views
0

我想從一個已經傳入一個集合的對象輸出一個字符串。下面這行是我的問題所在。它輸出[alex,jane]但格式正確,我相信它應該在alex jane輸出。即沒有逗號分隔值和數組中的括號。嘗試從一個對象傳入一個集合輸出一個字符串

System.out.print(module.getStudents() + " "); 

我已經嘗試了各種解決方案,包括:

System.out.prinf(%s, module.getStudents() + " "); 

System.out.prinln(module.getStudents().[whatever Netbeans makes available] + " "); 

爲了幫助你更好地理解這個問題。到目前爲止,該應用程序的想法是讓用戶搜索一個mosule並返回所有連接到它的學生。完整的源代碼欄上的驅動程序是:

import java.util.*; 

public class Control { 

public void run() { 

    Student jane = new Student("jane"); 
    Student alex = new Student("alex"); 

    Set<Student> students = new HashSet<Student>(); 
    students.add(jane); 
    students.add(alex); 

    Module ufce1 = new Module("UFCE1"); 
    Module ufce2 = new Module("UFCE2"); 

    Set<Module> modules = new HashSet<Module>(); 
    modules.add(ufce1); 
    modules.add(ufce2); 

    jane.addModule(ufce1); 
    jane.addModule(ufce2); 
    alex.addModule(ufce2); 

    ufce1.addStudent(jane); 
    ufce2.addStudent(jane); 
    ufce2.addStudent(alex); 

    System.out.println("Search module code: "); 

    Scanner scan = new Scanner(System.in);   

    scan = new Scanner(System.in); 
    String searchModule = scan.nextLine().trim(); 

    for (Module module : modules) { 
     if (searchModule.equalsIgnoreCase(module.getName())) { 

      Iterator it = students.iterator(); 
      Student student = (Student) it.next(); 
      if (student.getModules().contains(module)) { 
       System.out.print(student + " "); 
      } 
     } 
    } 


} 
} 

模塊類:

import java.util.HashSet; 
import java.util.Set; 

public class Module { 
private String name; 
private Set<Student> students = new HashSet<Student>(); 

public Module(String name) { 
    this.name = name; 
} 

public String getName() { 
    return name; 
} 

public void addStudent(Student student){ 
     students.add(student); 
} 

public Set<Student> getStudents() { 
    return students; 
} 

@Override 
public String toString() { 
    return name; 
} 

} 

學生類別:

import java.util.HashSet; 
import java.util.Set; 

public class Student { 
private String name; 
    private Set<Module> modules = new HashSet<Module>(); 


public Student(String name) { 
    this.name = name; 

} 

public String getName() { 
    return name; 
} 

public void addModule(Module module){ 
     modules.add(module); 
} 

public Set<Module> getModules() { 
    return modules; 
} 

@Override 
public String toString() { 
    return name; 
} 

} 

回答

2

當你做到這一點System.out.print(module.getStudents() + " ");你隱式調用上的toString方法HashSet實例。因此,爲了得到你想要的格式,你有2種選擇:

  1. 遍歷集合並打印你想要
  2. 子類HashSet的方式和覆蓋的toString顯示你想要的方式。
+0

我已經添加了一個迭代器的代碼。這就是你的建議。這將打印出所有學生,無論他們是否在該模塊上 – newToJava

+0

是的,因爲您沒有進行任何過濾。你需要做的是:'學生student = it.next(); if(student.getModules()。contains(module)){System.out.print(student +「」);}' – Chris

+0

@ Chris謝謝。但是當我之前添加這個:Iterator it = students.iterator();它說不兼容的類型? – newToJava

0

問題是getStudents返回Set對象(HashSet,具體而言)。 HashSet,反過來,繼承AbstractCollection一個toString()方法,其行爲如下:

返回此集合的字符串表示。字符串表示由集合元素的列表組成,它們按其迭代器返回的順序包含在方括號中(「[]」)。相鄰元素由字符「,」(逗號和空格)分隔。通過String.valueOf(Object)將元素轉換爲字符串。

您需要編寫自己的方法將一組學生轉換爲您想要的格式,否則您可以將默認的toString實現所返回的值提取出來。

0

括號和逗號來自Set上的toString()調用。如果你查看這個方法的源代碼,你會看到它添加了這些。您可以覆蓋模塊類中Set的toString()方法,或者不直接打印Set,而是手動循環遍歷所有元素並逐個打印它們

0

@Chris爲解決您的問題提供了一個出色的解決方案。然而,有另外一個,我看到越來越容易實現,它是:

public String formatOutputString(){ 
    String setStrings = module.getStudents(); 
    // Get rid of opening bracket 
    String formatedString = setStrings.replace("[", ""); 
    // Get rid of closing bracket 
    formatedString = setStrings.replace("]", ""); 
    // Replace commas by spaces 
    formatedString = setStrings.replace(",", " "); 
    return formatedString; 
} 
相關問題