sinAnshul sinha

Greenhorn
+ Follow
since Mar 07, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by sinAnshul sinha

[ UD: removed inappropriate suggestion ]
[ March 28, 2008: Message edited by: Ulf Dittmer ]
16 years ago
It is entirely up to you if you want to buy additional material or not. However if I would have been in your place I would have worked on K&B book again and again instead of getting new material.
Thankx charmy....appreciate your quick help..
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
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