Java Reference
In-Depth Information
for(E item : items) {
add(item);
}
}
// Rest of the code the same as in the BinaryTree definition
// in the previous example...
}
Directory "TryParameterizedConstructor"
The only changes from the previous version are the addition of the constructor that accepts an array as
an argument and the definition of the no-arg constructor, which is not supplied by the compiler when
you explicitly define a constructor of your own. Put this source file in a new directory and copy the
LinkedList.java , Person.java , and Manager.java files from the previous example to this directory.
You can add the following source file to try out the parameterized constructor:
public class TryParameterizedConstructor {
public static void main(String[] args) {
Manager[] managers = {new Manager("Jane",1), new Manager("Joe",3),
new Manager("Freda",3), new Manager("Bert",
2),
new Manager("Ann", 2),new Manager("Dave",
2)};
// Create the tree with an array of managers
BinaryTree<Person> people = new BinaryTree<>(managers);
// Create and add some Person objects
Person[] persons = {
new Person("Will"), new Person("Ann"), new
Person("Mary"),
new Person("Tina"), new Person("Stan")};
for(Person person : persons) {
people.add(person);
}
listAll(people.sort());
}
// List the elements in any linked list
public static <T> void listAll(LinkedList<T> list) {
for(T obj : list) {
System.out.println(obj);
}
Search WWH ::




Custom Search