• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

arrays - varied locations allowed?

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic