Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

assigning values to a list of variables  RSS feed

 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a list of say 10 currencies and am concerned about an amount associated with each

Initially I zero all currency amounts, then
In my code I
1) ascertain which currency I am currently dealing with
2) add or subtract a certain amount to/from that currency amount

If I have the list of currencies in a string such that string[0] is the first currency and string[9] is the last
and suppose each string has two components, namely (i) name and (ii) amount
After 1) above I may have USD for instance
How do I quickly ascertain which of string[0] to string[9] is the USD + amount string ?

Bob M
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't. That sounds a really error‑prone way to do it. The last thing you want to do is try to pull Strings apart.
I suggest you need a Currency class, then you can make a Currency[] array.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Matthews wrote:I have a list of say 10 currencies and am concerned about an amount associated with each

And further to what Campbell said, I'd say that a list (or array) is NOT what you want. If you have a set of currencies, and an amount associated with each one, I'd say that a Map would be a much better way to store them.

Presuming you're familiar with Maps that is.

Winston
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right about Maps. A bit more thought would have led me to the same conclusion.
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston & Campbell

I know nothing about Maps but will look into them

regards

Bob M
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!