[英]Trying To Output A String From An Object Passed Into A Set
我正在尝试从已传递到集合的Object中输出字符串。 以下行是我的问题所在。 它输出[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;
}
}
当你这样做System.out.print(module.getStudents() + " ");
你隐式调用HashSet实例上的toString方法。 因此,要获得所需的格式,您有两个选择:
问题是getStudents
返回一个Set
对象(一个HashSet
,具体而言)。 反过来, HashSet
从AbstractCollection
继承了一个toString()
方法,其行为如下:
返回此集合的字符串表示形式。 字符串表示由一个集合元素的列表组成,它们的迭代器返回它们的顺序,用方括号(“[]”)括起来。 相邻元素由字符“,”(逗号和空格)分隔。 通过String.valueOf(Object)将元素转换为字符串。
您需要编写自己的方法来将一组学生转换为您想要的格式,否则您可以调整默认的toString
实现返回的值。
括号和逗号来自Set上的toString()
调用。 如果您查看此方法的源代码,您将看到它添加了这些。 您可以覆盖Module类中Set的toString()
方法,或者不直接打印Set,而是手动循环遍历所有元素并逐个打印它们
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.