9three Posted March 31, 2009 Share Posted March 31, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/151973-solved-java-array-a-z/ Share on other sites More sharing options...
Yacoby Posted March 31, 2009 Share Posted March 31, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/151973-solved-java-array-a-z/#findComment-798096 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151973-solved-java-array-a-z/#findComment-798136 Share on other sites More sharing options...
Yacoby Posted March 31, 2009 Share Posted March 31, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/151973-solved-java-array-a-z/#findComment-798155 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 Yeah errr... I was talking on the phone to someone and got distracted (obviously). lol. Quote Link to comment https://forums.phpfreaks.com/topic/151973-solved-java-array-a-z/#findComment-798207 Share on other sites More sharing options...
9three Posted April 1, 2009 Author Share Posted April 1, 2009 thanks Quote Link to comment https://forums.phpfreaks.com/topic/151973-solved-java-array-a-z/#findComment-798279 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.