• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

EL question with [] operator

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<%
request.setAttribute("vals",new String[]{"1","2","3","4"});
request.setAttribute("index","2");
%>

which 3 EL expressions are valid and evaluate to "3"?

a)${vals["2"]}
b)${vals.index}
c)${vals[index]}
d)${vals.(vals.index)}
e)${vals[vals[index-1]]}
f)${vals}[index]
 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a,c,e
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bindu Lakhanpal wrote:a,c,e



how come e? what is vals[index-1] evaluate to? vals["2"-1] ?
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think vals[index-1] eveluates to vals[2-1] ie vals[1]
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bindu Lakhanpal wrote:I think vals[index-1] eveluates to vals[2-1] ie vals[1]



by the way can a string value and an integer together in a arithmetic operation..i mean "2"-1 ?

Thank You.
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is A, C, E

for the doubt of answer E

${vals[vals[index-1]]}

index-1 evaluates to 1
vals[1] evaluates to 2
vals[2] evaluates to 3

So the answer is correct.


Answer two gives a syntax error it will not work. it would work if vals was a map and had a key value equal to (index)
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

index-1 evaluates to 1



is "2"-1 evaluate to 1 ? in normal java this is not gonna work right?
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes "2" - 1 evaluates to 1 this is Expression Language which is not as the Java language.

of course if you put "2" -1 in Java the compiler will explode with anger but in EL it will work just fine
 
The City calls upon her steadfast protectors. Now for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic