• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using [] operator with an array

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is {musicList["1"]} & ${musicList[1]} same?

For numerical indexes Yes.
Its not the same for keys i.e. {musicList["a"]} & ${musicList[a]} are different...
 
gopal kishan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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].
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic