Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ 1st edition, help me understand this

 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • 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 ?
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Vishal,

it makes sens
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic