• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock exam by sreenivasa kumar majji - Q16

 
paul wheaton
Trailboss
Pie
Posts: 21390
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21390
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder why the creators of the AWT decided to do this?
 
zhang feng
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic