Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

HFSJ 1st edition, help me understand this

 
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 ?
 
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
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic