Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics doubt in K&B p.574

 
sinAnshul sinha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Charmy Madhvani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
sinAnshul sinha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx charmy....appreciate your quick help..
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic