Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL question with [] operator

 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • 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]
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a,c,e
 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I think vals[index-1] eveluates to vals[2-1] ie vals[1]
 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • 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.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • 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: 348
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic