• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays - varied locations allowed?

 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create several arrays to hold different pieces of data. The data will be number related and it would be convenient to make each number in the array be appropriate to the string held there.

for instance
month[1] = "January"
month[2] = "February"

*assume I don't need these months*

month[10] = "October"
month[11] = "November"

Can I just omit 3 - 9 in the array or do the numbers have to be sequential?

Thanks for any help.
Matt
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array indices have to be sequential, but you don't necessarily have to use arrays. You could use a Map to associate numbers (or Strings or anything) with Strings (or numbers or anything else.)



The keys and values in a Map can be any Java objects. The only complication is that you have to cast the result of the "get" operation to the correct type -- unless you're using generics in Tiger.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While the numbers in the array are sequential, you don't have to use them all. It is perfectly valid to only assign objects to the four indicies you list.

But, Ernest is right (as usual) in that an Array might not be the best way to do this.
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, instead of

month[1] = "January"

I could use

month[jan] = "January"

??
I think the answer is yes. It is just a variable naming that location in the array. Am I wrong?
[ April 13, 2005: Message edited by: Matt Fielder ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic