First of all I am sorry if don't know how to explain my question properly and if the title is not fully correct or not clear enough. This is my first stackoverflow post. I will try to explain my beginner question with example:
I have following Student class:
public class Student {
private String name;
private int matrikelnummer;
public static int STUDENT_COUNT;
public Student(String name, int matrikelnummer){
this.name = name;
this.matrikelnummer = matrikelnummer;
STUDENT_COUNT++;
}
public String toString() {
return "Student Nr.: " +matrikelnummer+" Name: "+name;
}
}
and another class Tutorium:
public class Tutorium {
private int raumNr;
private String tutor;
private String fach;
private Student[] teilnehmer;
public Tutorium(int kapazitaet, int raumNr, String tutor, String fach) {
this.raumNr = raumNr;
this.tutor = tutor;
this.fach = fach;
teilnehmer = new Student[kapazitaet];
}
public String toString() {
if (teilnehmer == null) {
return "Noch keine Teilnehmer";
}
String out = "Tutorium " + fach + " Bei " + tutor + " in " + raumNr + " " + "\n";
return out + --- ; //here i want to print all array element from teilnehmer
}
}
For eg if have following main:
public class TutoriumsVerwaltung {
public static void main(String[] args) {
//Studenten und Tutorien erzeugen.
Student[] studs = {new Student("Tim", 333333),
new Student("Anna", 444444),
new Student("Lisa", 345987),
new Student("Karl", 336292),
new Student("Elisabeth", 999999)};
Tutorium[] tuts = {new Tutorium(5, 6057, "Tobias", "PPR-J"),
new Tutorium(2, 6057, "Roland", "PPR-C"),
new Tutorium(9, 6051, "Max", "PPR-J")};
//Infos ueber Tutorien und Studenten ausgeben.
System.out.println("Es gibt "+Student.STUDENT_COUNT+" Studenten.");
for (Student student : studs)
System.out.println(student);
for (Tutorium tutorium : tuts)
System.out.println(tutorium);
System.out.println("");
I want toString() method from Tutorium to print in following format:
Tutorium PPR−J bei Tobias in Raum 6057
Teilnehmer:
Student Nr.: 333333 Name: Tim
Student Nr.: 444444 Name: Anna
Student Nr.: 345987 Name: Lisa
I have tried creating getters/setters (maybe not correctly?) but without success. Would someone care explaining me my problem and solution to it.
What you want is to basically get every element from a collection (array in this case). You can easily achieve this by using a for-loop .
Code:
for(Student student : teilnehmer) {
// If an array isn't full it contains null elements.
if (student != null) {
out += student.toString() + "\n"; // '\n' means newline
}
}
modify the code from this:
public String toString() {
if (teilnehmer == null) {
return "Noch keine Teilnehmer";
}
String out = "Tutorium " + fach + " Bei " + tutor + " in " + raumNr + " " + "\n";
return out + --- ; //here i want to print all array element from teilnehmer
}
to
@Override
public String toString() {
// if (teilnehmer == null) {
// return "Noch keine Teilnehmer";
// }
String out = "Tutorium " + fach + " Bei " + tutor + " in " + raumNr + " " + "\n";
return out + Arrays.toString(teilnehmer);
}
note that I commented out this (teilnehmer == null)
because that condition will never met, you are creating an instance in the constructor so teilmehmer will never be null
you need to set the Teilnehmer:
public static void main(String[] args) {
// Studenten und Tutorien erzeugen.
Student[] studs = { new Student("Tim", 333333), new Student("Anna", 444444), new Student("Lisa", 345987),
new Student("Karl", 336292), new Student("Elisabeth", 999999) };
Tutorium[] tuts = { new Tutorium(5, 6057, "Tobias", "PPR-J"), new Tutorium(2, 6057, "Roland", "PPR-C"),
new Tutorium(9, 6051, "Max", "PPR-J") };
// Infos ueber Tutorien und Studenten ausgeben.
System.out.println("Es gibt " + Student.STUDENT_COUNT + " Studenten.");
//here set the array
for (Tutorium tutorium : tuts) {
tutorium.setTeilnehmer(studs);
}
for (Student student : studs)
System.out.println(student);
for (Tutorium tutorium : tuts)
System.out.println(tutorium);
System.out.println("");
}
and define the setter:
public void setTeilnehmer(Student[] teilnehmer) {
this.teilnehmer = teilnehmer;
}
也许您可以尝试覆盖Student类和TutoriumsVerwaltung类中的toString方法。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.