Forums Register Login

using [] operator with an array

+Pie Number of slices to send: Send
In chapter no.8(Head First Servlet & Jsp- "Scriptless JSPs"), there is a topic USING THE [] OPERATOR WITH AN ARRAY.

In the Example,,,


so my Question is why we use Quotes around the array index?

Can we use ${musicList[1]}??(the Quotes are removed)

Is {musicList["1"]} & ${musicList[1]} same?
+Pie Number of slices to send: Send
 

Is {musicList["1"]} & ${musicList[1]} same?


For numerical indexes Yes.
Its not the same for keys i.e. {musicList["a"]} & ${musicList[a]} are different...
+Pie Number of slices to send: Send
Kunal,
The Symbol [] used to access map, bean, array, list.

In case of array you no need to use ${array["0"]} , you can simply use ${array[0]}, but how do you access map & bean? . There is no index concept in these two, map is based on key and bean is a POJO where you can retrieve the properties.

For accessing bean you have 2 options.
1.

or

2.

Thats why you have option for array to access the index item , either by using ["0] or [0].
+Pie Number of slices to send: Send
In the same chapter, i am refering to Q.no 6. page no.424.. There is a statement


One of the option for the question is: ${list['listIdx' +1]}
Though this option is incorrect, the explaination given is : It is Incorrect because EL tries to coerce 'listIdx' to a Long which is invalid

What does it mean?
+Pie Number of slices to send: Send
There is no String concatenation in EL, so when you write 'listIdx'+1, then String concatenation is not done to get 'listIdx1', instead EL tries to add the two values thus tries to convert 'listIdx' to a long value...
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1018 times.
Similar Threads
Unable to understand the solution on EL
Small Query regarding EL
Unable to display the content in array(created in the servlet)using JSP!
HFSJ EL e.g. page 371
EL and quotes (HFSJ pg373)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:45:51.