package pl.codegym.task.task20.task2013;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.List;
/*
Externalizable dla klasy Person
*/
public class Solution {
public static class Person implements Externalizable{
private String firstName;
private String lastName;
private int age;
private Person mother;
private Person father;
private List<Person> children;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public Person() {}
public void setMother(Person mother) {
this.mother = mother;
}
public void setFather(Person father) {
this.father = father;
}
public void setChildren(List<Person> children) {
this.children = children;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(mother);
out.writeObject(father);
out.writeChars(firstName);
out.writeChars(lastName);
out.writeInt(age);
for(Person child : children)
out.writeObject(child);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
firstName = in.readLine();
lastName = in.readLine();
father = (Person)in.readObject();
mother = (Person)in.readObject();
age = in.readInt();
while(in.available() < 0)
children.add((Person) in.readObject());
}
}
public static void main(String[] args) {
}
}
What is bad?
Rozwiązane
Komentarze (7)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Damian Siadaczka
3 maja 2020, 21:24
Ok, I need rest :D
Thanks guys
0
Misiu
3 maja 2020, 21:09rozwiązanie
Order of writings and readings should be the same.
+2
Norbert Backend Developer
12 sierpnia 2020, 15:55
Czy możesz jeszcze jakoś podpowiedzieć mam tą samą koeljność a i tak mi nie działa nie wiem co mogę więcej zrobić
0
Misiu
12 sierpnia 2020, 16:12
children - zapisuję i odczytuję jako obiekty, bez pętli.
0
Norbert Backend Developer
12 sierpnia 2020, 16:23
Tak też zrobiłem a cały czas mi nie działa
0
Misiu
12 sierpnia 2020, 18:28
No to jeszcze zapisywanie i odczytywanie zmiennych typu string. Zapisywanie i odczytywanie jako obiekty.
Przy odczycie pamiętać o rzutowaniu typu. Zarówno tych stringów, jak i wcześniej - children to List<Person>.
0
Azamat Aminov Backend Developer
3 maja 2020, 20:59przydatny
Remove loops:
+1