The Java Platform Java Runtime Environment Data Types Control Statemets Methods Arrays Classes and Objects Inheritance Constructor Interface Packages & Access Modifiers Java Collections Framework![]() What are Collections![]() The Java Collections Framework![]() Benefits![]() Collection hierarchy![]() The Collection Interface![]() Types of Collection![]() Iterators![]() Using Set![]() Using Map![]() Example | ExampleIn this example, we are going to re-implement the Drawing class in the previous section. package drawing;
import java.util.*;
import graphicalobject.GraphicElement;
public class Drawing {
// GraphicElement[] element = new GraphicElement[10];
Collection elements = new Vector(); //or ArrayList or HashMap
// private int count = 0;
public void addElement(GraphicElement elem) {
// element[count++] = elem;
elements.add(elem);
}
public int getElementCount() {
// return count;
return elements.size();
}
public void display() {
// for (int i = 0; i < count; i++) {
// element[i].display();
// }
for (Iterator it = elements.iterator(); it.hasNext(); ) {
((GraphicElement)it.next()).display();
}
}
} |