• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to print values of a list  RSS feed

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i have list of employees but i want to know about some 79 th employe's salary.


may i know how to get from a list. the below mentioned code does not work

 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rauhl ,
your code will not work because the return type of List.get(int index) is Object ( not Employee as in your code ) , so before you can call getSalary() on it you have to cast the returned value to Employee. something

((Employee)list.get(int index) ).getSalary()

OR
you can use a Generic List of type Employee so the cast is automatically done for you
[ March 20, 2008: Message edited by: abhishek pendkay ]
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by abhishek pendkay:
Hi Rauhl ,
your code will not work because the return type of List.get(int index) is Object ( not Employee as in your code ) , so before you can call getSalary() on it you have to cast the returned value to Employee. something

((Employee)list.get(int index) ).getSalary()

OR
you can use a Generic List of type Employee so the cast is automatically done for you

[ March 20, 2008: Message edited by: abhishek pendkay ]


thanks alot.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree. If you set up a List<Employee>, you don't need the cast.

And list.get(79) doesn't get the 79th member; it gets the 80th, which is in fact what you asked about in your previous post.
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
originally posted by Campbell Ritchie
Disagree. If you set up a List<Employee>, you don't need the cast.


maybe you need to read others post's properly before giving your ideas... I have already spoken about using Generics
and as far as accessing the 79th element I have just modified the code posted by Rauhl which was list.get(79)
[ March 20, 2008: Message edited by: abhishek pendkay ]
[ March 20, 2008: Message edited by: abhishek pendkay ]
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am sorry, you did say to use a generic List.

He has already asked the same question on another thread. But list.get(79) doesn't get the 79th member; it gets the 80th.
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
you dont have to say sorry.... and as far as list.get(79) not getting the 79th element i agree , but i belive his question was more to do with how to access elements ( any )of a list rathar then how to access the 79th element so I just modified the same code he originally posted
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

He was asking about how to get the 80th member in another thread where he had been told to use list.get(i); then he asked for the salary for the 80th member, so I told him, "list.get(79).getSalary();" Then he started a new thread . . .
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!