• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KB page no 547

 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please explin me why
System.out.println(test.contains(42));
System.out.println(test.contains("hihi"));

line one priting true and line 2 false.

Thanks in advance
Suma
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for those of use who do not have the book, could you elaborate a little more on the code?
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...
List<String> test = new ArrayList<String>();
String s = "hi";
test.add("string");
test.add(s);
test.add(s+s);
System.out.println(test.size());
System.out.println(test.contains(42));
System.out.println(test.contains("hihi"));
test.remove("hi");
System.out.println(test.size());
...

size() is called, and result, 3, is printed;
"string", "hi", and "hihi" are added to the list;
contains(42) is called, and the result, false, is printed;
contains("hihi") is called, and the result, true, is printed;
remove("hi") removes middle element;
size() is called and result, 2, is printed.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<String> test = new ArrayList<String>();
System.out.println(test.contains(42));


Here test is a list reference can only contain the Strings.
When what is the use of checking other types ( here 42 Integer) in test by using contains method?
why API written for contains method accepting the object of type 'Object'.
Can any one calrify me about this?

rami
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic