aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock exam by sreenivasa kumar majji - Q16 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock exam by sreenivasa kumar majji - Q16" Watch "Mock exam by sreenivasa kumar majji - Q16" New topic
Author

Mock exam by sreenivasa kumar majji - Q16

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20642
    ∞


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?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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();

}
}
the output is
1
Banu
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20642
    ∞

I wonder why the creators of the AWT decided to do this?
zhang feng
Greenhorn

Joined: Dec 16, 2002
Posts: 19
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mock exam by sreenivasa kumar majji - Q16