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
https://forums.phpfreaks.com/topic/151973-solved-java-array-a-z/
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);

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.