• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in EL [] operator

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<%
List list = new ArrrayList();
list.add("a");
list.add("2");
list.add("c");

request.setAttribute("list",list);
request.setAttribute("listIdx","1");
%>
${list[list['listIdx']]}

What will be the value of output?

I thought that list['listIdx'] will return 0, since 'listIdx' is in quotes.(Quoted 'listIdx' is not an index of list, so the default value 0). & hence list[0] returns 'a'.

But according to HFSJ "c" is the correct answer (Page: 428)
I think,the term inside [] evaluated only if it is not in quotes.[Page 396, 7th bullet point].

Pls advice where I misunderstood. Thanks.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
list['listIdx'] will throw an exception since 'listIdx' is a String, and not an integer used to retrieve the values in the list [i.e. get(int index)]

I checked the errata of HFSJ at http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed and it looks like the correct option was

[ February 21, 2007: Message edited by: Hunny Lee ]
 
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic