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

HFSJ 1st edition, help me understand this

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At page 428 question 7 in this book it's said that

${foo["5"]} is valid syntax if foo is a Map

I thought that we can use indexes only for lists and arrays ?

am I right ?
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Given if foo is a map:

1) ${foo["5"]} is valid syntax , here 5 will be a key into map.

2) ${foo.5} is not a valid syntax as whatever comes after dot must follow the identfier rules (as per HFSJ)

[See , maps and beans can use dot as well as [] operator (given whatever comes after dot must be a valid identifier)
Arrays and lists can only use [] operator , as indices will always be numerals and wont work with dot ]


You are right : For sure , we have numerals(as indices) with lists and arrays only but we can have numerals as keys in maps too but they will be accessed only using [] , not dot .

I hope , it makes sense.


 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vishal,

it makes sens
 
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic