If you add() the same button twice, it will appear only once? So the add() method checks to see if that object is already in its collection and the collection is a Set, yes?
yes, you are correct. Test this code to clarify the concept import java.awt.*; public class TestAddTwice extends Frame { Panel pnl; public TestAddTwice() { super(); pnl = new Panel(); Button btn = new Button("Test"); pnl.add(btn); pnl.add(btn); System.out.println(pnl.getComponentCount()); add(pnl); } public static void main(String args[]) { TestAddTwice test = new TestAddTwice(); test.show();
I wonder why the creators of the AWT decided to do this?
zhang feng
Greenhorn
Joined: Dec 16, 2002
Posts: 19
posted
0
reply yes let's write fellow class
Guess output what? true false false [1,2,3] Set's add() method that will return boolean value , show the object if add successful i understand this way i think that the object added the set pass the first reference,before add that pass the object's other reference ,so the objectself cannt the into the set pass the second reference [ September 07, 2003: Message edited by: zhang feng ]