• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating an Array question  RSS feed

 
Mike Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to make an array that is subscripted by the letters 'A' through 'Z'. How do I do this? From what I can tell, I need to use the ASCII code for the letters, 65 through 90. The question is how do I make an array *not* start from 0? I want the array to go from 65 to 90. Is this even possible? Or should I just create an int array[] = new int [90] (giving it 90 slots to work with), just knowing that slots 0-63 will be unused? This seems do-able to me, but I'm just wondering if there's a way to not waste any space at all, and condense the array down to just what I need.

Essentially what this program will do is read in a user-inputted string and tell how many letters came up using an array, and how many vowels came up, etc.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could use an array index of "letter - 'A'" instead of just the letter. This way, you only use 26 array elements.

Henry
 
Mike Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive my newness to Java, but how do you make an array index of "letter - 'A'"?

This is a homework problem, and so far I'm using just an array of 90 and it's working well. I'll probably just turn it in this way, but for future reference, I'd like to know char array[] = new char [letter - 'A'] ??
[ July 30, 2006: Message edited by: Mike Smith ]
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By subtracting the value of 'A' from the index that you are current using, you don't waste the first 60+ array slots.



By doing this, you only need to use the first 26 slots, and hence, only need to allocated 26 slots.

Henry
 
Mike Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hah, genius, thanks Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!