• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with the Scriptless JSps Page 378 question (Code Magents) from the Head First book 1st edt

 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have a Problem with the Scriptless JSps Page 378 question (Code Magents) from the Head First book 1st edition of chapter 8.
The same problem is in page 380 of 2nd edition.

Solution given is:


I think the solution should be

or




Can someone please explain me why there is no " " inside the [1] and [2]?

Also, why do we have to use the .name after [] to access the name of the toy when I thought we can directly do so using toys[0]?

Thanks in advance for your help and cooperation.

 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inside [], you can access indexed elements with or without "". So If I have an array in page scope named myArr, then ${myArr[0]} and #{myArr["0"]} mean the same thing.

As far as the second solution goes, I can't say whether it is wrong or not as I don't have the book right now. But the thing is, if you use .name on the toy objects, then it will display the value of the name property of the toy objects. If you don't include the .name, then the toString method of the toy objects will be called and whatever that returns, will be displayed. So if the toString method of Toy class returns the name of the toy, then the last code will also produce the correct output...
 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Thanks..But I have some confusion still..

I think [1] and ["1"] are not the same thing as [mybooks] and ["mybooks"] are different. Correct me if I am wrong
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:inside [], you can access indexed elements with or without "". So If I have an array in page scope named myArr, then ${myArr[0]} and #{myArr["0"]} mean the same thing.

See what I wrote again. For indexed elements i.e. ArrayList, Array etc, [1] and ["1"] mean the same thing...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic