This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in EL [] operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in EL [] operator" Watch "Doubt in EL [] operator" New topic
Author

Doubt in EL [] operator

Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
<%
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.


Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
Hunny Lee
Greenhorn

Joined: Feb 17, 2007
Posts: 23
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in EL [] operator