aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes using [] operator with an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "using [] operator with an array" Watch "using [] operator with an array" New topic
Author

using [] operator with an array

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
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?


kunal
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

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

For numerical indexes Yes.
Its not the same for keys i.e. {musicList["a"]} & ${musicList[a]} are different...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
gopal kishan
Ranch Hand

Joined: Feb 23, 2005
Posts: 99
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

Joined: Jun 05, 2010
Posts: 622
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using [] operator with an array