This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics doubt  in  K&B  p.574 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics doubt  in  K&B  p.574" Watch "Generics doubt  in  K&B  p.574" New topic
Author

Generics doubt in K&B p.574

sinAnshul sinha
Greenhorn

Joined: Mar 07, 2008
Posts: 5
Following is the code form K&B page 574.Also see Errata for same page.
I have modified it a bit so as to run it on compiler.



import java.util.*;

class listDemo{

public static List<Dog> getDogList(){

List <Dog> dog = new ArrayList<Dog>();

dog.add(new Dog("jacky"));
dog.add(new Dog("jackyDAD"));

return dog;

}


public static void main(String a[]){


Dog d=getDogList().get(0);
getDogList().add(new Dog("JackyMom"));

System.out.println(getDogList().size());
}
}





class Dog{
String n;
Dog(String name)
{
n=name;
}
public String toString()
{

return n;

}
}
//

(1)
my doubt is in the syntax "getDogList().get(0)"...its new to me kindly explain/elaborate.
When getDogList() method does not have any method called "get()" how does get() version of List is Invoked....what if we have 2 Lists in
getDogList() ...wont there be ambiguity?

(2)
And if I write getDogList().add(new Dog("JackyMom")); from the calling function i.e main()
the list does not gets populated.
Only Jacky and JackyDad gets inserted ...JackyMom(added in main) does not enter in the List....why?

From calling fn i.e main() get() it is working fine....but add() is not working...why?

(3) if I do s.o.p(getDogList().add(new Dog("JackyMom")));---the output is true....but list does not gets populated
Rahul Singhai
Greenhorn

Joined: Mar 04, 2008
Posts: 19
Replace
System.out.println(getDogList().size());
with
System.out.println(getDogList());

evrytime "getDogList()" is called. it is creating a new list and there will be only 2 elements.


<br />
SCJP - 93%
Warm Regards,<br />Rollicking
Charmy Madhvani
Ranch Hand

Joined: Dec 26, 2007
Posts: 59
Hi sinAnshul ,
Answers in line for you

(1)
my doubt is in the syntax "getDogList().get(0)"...its new to me kindly explain/elaborate.
When getDogList() method does not have any method called "get()" how does get() version of List is Invoked....what if we have 2 Lists in getDogList() ...wont there be ambiguity?


--List interface has a method get(index). Now your getDogList() method returns a List. so getDogList().get(0) means get the 0th element of the List returned by getDogList() method. It is same as
List dogList=getDogList();
Dog d=dogList.get(0);


2)
And if I write getDogList().add(new Dog("JackyMom")); from the calling function i.e main()
the list does not gets populated.
Only Jacky and JackyDad gets inserted ...JackyMom(added in main) does not enter in the List....why?


Every time you invoke a method getDogList() you are creating and returning a new List of Dog from that method.
Dog d=getDogList().get(0); // List 1
getDogList().add(new Dog("JackyMom")); //List 2
System.out.println(getDogList().size()); //List 3

So you are adding JackyMom on List 2 and Printing List 3. Hence the size is displayed as 2.

From calling fn i.e main() get() it is working fine....but add() is not working...why? Everything is working fine.

(3) if I do s.o.p(getDogList().add(new Dog("JackyMom")));---the output is true....but list does not gets populated I think I have already answered this question..


SCJP 5.0<br />SCWCD 5.0
sinAnshul sinha
Greenhorn

Joined: Mar 07, 2008
Posts: 5
Rahul thats what I want to know---

why cant I add element in list from main() just like i have retrieved elements from main()

main(){
Dog d=getDogList().get(0);-------------------working
getDogList().add(new Dog("jackyMom"));----------not working
sinAnshul sinha
Greenhorn

Joined: Mar 07, 2008
Posts: 5
Thankx charmy....appreciate your quick help..
agilemanoj kumar
Ranch Hand

Joined: Mar 07, 2008
Posts: 70
import java.util.*;
class listDemo{
public static List <Dog> dog = new ArrayList<Dog>();
public static List<Dog> getDogList(){
//List <Dog> dog = new ArrayList<Dog>();
dog.add(new Dog("jacky"));
dog.add(new Dog("jackyDAD"));
return dog;
}
public static void main(String a[]){
Dog d=getDogList().get(0);//First call to getDogList() fnk. 2 dogs are added
getDogList().add(new Dog("JackyMom"));//2nd call, 2 more(+1 here) are added.
System.out.println(getDogList().size()); //again 2 more. O/P ( 2+2+1+2=7) .
System.out.println();
}
}
class Dog{
String n;
Dog(String name)
{
n=name;
}
public String toString()
{
return n;
}
}

// Make your dog variable which is defined in getDogList funk member variable of class listDemo. In bold it is shown.

[ March 07, 2008: Message edited by: agilemanoj kumar ]
[ March 07, 2008: Message edited by: agilemanoj kumar ]

Manoj Kumar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics doubt in K&B p.574
 
Similar Threads
K&B SCJP5 pg 574 - Generics
hashMap doubt
Interface Set's get() method>>>>
Comparator Doubt
Doubt in HashMap