Whenever in need of a sane string representation of your instance, override the toString() method for the given class. This makes sure that each class is responsible for giving out its own representation without having to write the same temporary code again and again. For pretty printing arrays, convert them first to a list using Arrays.asList(yourArray) .
public class Family {
private Adult father;
private Adult mother;
private Child[] children;
public String toString() {
StringBuilder buf = new StringBuilder(256);
buf.append("Father {").append(father)
.append("} Mother {").append(mother)
.append("} Children {").append(Arrays.asList(children))
.append("}");
return buf.toString();
}
}
public class Adult {
private String name;
// other instance variables go here
public String toString() {
StringBuilder buf = new StringBuilder(128);
buf.append("Name: ").append(name);
return buf.toString();
}
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
> aah! its seems we're not communicating! You are complicating the code further
Yes, rightly said, because it is what you asked for in the first place. Re-read your first post:
any help on how to modify the code so that it can print out all the family members.
> NOTE: Other sections have no problem except this array section.
There is no `array section' here; it is how the toString() to the ArrayList class is implemented.
There are two ways of doing it; either the way mentioned in my first post or writing a method which takes the List , loops over it and extracts the name, either printing them or adding them again to a new List which then gets displayed.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
to get an output like this:
Movie goers: [John , Mary, Frank, Lisa]
as in the names of Adult and Child given.
your code works and does exactly what you programmed it to do. just as ~s.o.s~ pointed out, whenever you try to print an Object or a list of Objects, you'll call the toString method of the class.
since your Family class does not have such a method, you'll just see a String representation of the memory-address used by the Object. if you want to make a print-out of all the persons' names in the list, the best way to do so is by creating that toString method.
stultuske
Posting Sensei
3,110 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 432
Use the addAll method of the ArrayList class; it's a good idea to consult the documentation once in a while, at least for the classes which you plan on using.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
> - You are trying to cast a List into a Limb. this cant work.
Yes, it's true; look at my previous post for the answer.
> You have not instatiated the limbs list, how can you then call its add() method
This comment seems weird since you have instantiated the ` limbs ' list in the Mammal constructor.
BTW, your implementation of removeLimbs is broken. Why do you require instanceof checks? Plus, those nested IF blocks are incorrect. You could have simply done a limbs.clear() and got away with it.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733