| Author |
Creating an Array question
|
Mike Smith
Greenhorn
Joined: Jul 28, 2006
Posts: 9
|
|
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
Joined: Sep 28, 2004
Posts: 16681
|
|
Well, you could use an array index of "letter - 'A'" instead of just the letter. This way, you only use 26 array elements. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Mike Smith
Greenhorn
Joined: Jul 28, 2006
Posts: 9
|
|
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
Joined: Sep 28, 2004
Posts: 16681
|
|
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
Joined: Jul 28, 2006
Posts: 9
|
|
Hah, genius, thanks Henry
|
 |
 |
|
|
subject: Creating an Array question
|
|
|