Jump to content

[SOLVED] [Java] Array A - Z


Recommended Posts

Hey,

 

I'm learning Java at school and our professor has given us an extra credit assignment.

 

He is asking to create an array A - Z.

 

Now I could do it the long way and just list them one at a time, but I'm pretty sure there is a method I could use to create a range from x letter to x letter.

 

Can anyone lend a hand?

Link to comment
Share on other sites

I am teh suck at Java, however, you may just be able copy the values 65 to 90, into a array of bytes. (A - Z in the ASCII table). It is what I would do in C++.

 

Something like this might work:

java.util.List<char> charArray = new ....;
for ( char c = 65; c <= 90; ++c ) charArray.add(c);

Link to comment
Share on other sites

If you want to do it as a letter range, you could do something like (borrowing and changing Yacoby's code kinda):

 

char[] alphabet = new char[26];

for(char c = 'a'; c <= 'z'; ++c) {

    ++c;

}

 

 

Not really an advantage.  Just a little (very little) different way ;p.

Link to comment
Share on other sites

You mean this right? (Unless I am missing some key piece of java knowledge)

char[] alphabet = new char[26];
for(char c = 'a'; c <= 'z'; ++c) {
    alphabet[c - 'a'] = c;
}

 

Not really an advantage.  Just a little (very little) different way ;p.

Far better code. Much clearer as to the purpose

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.