Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in EL [] operator

 
Sahul Yasin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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.
 
Hunny Lee
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic