Welcome to the Newschoolers forums! You may read the forums as a guest, however you must be a registered member to post. Register to become a member today!
Do you guys go to Montana State? I'm really interested in going there and studying computer science. Im a senior so ill be there next year. What is the best language to learn? I'm taking an intro to java class right now and I'll probably take c++ next semester. Also, are the compsci classes hard/require a lot of studying? Thanks
Ski_The_EWhere do I start if I have no idea what I'm doing? I'd love to learn how to code/program, but I find that it's really hard to get into.
RedPandaAnyone else taking AP Computer science? Fuck java.
cool_nameNow I want to code something again, somebody give me a problem that is solvable in java
RedPandaHere is my homework for tonight:
Write and test a method that takes an array list named 'words', which contains strings of alphabetic characters, throws them into 26 "buckets", according to the first letter, and returns the ArrayList of buckets. Each bucket should be represented by an ArrayList of the type "String". The first bucket should contain all the strings from 'words' that start with an 'a', in the same order as they appear in 'words'; the second bucket should contain all the strings that start with a 'b', and so on. Your method should traverse the list 'words' only once and leave it unchanged.
I'm pretty lost on this one.
iLLbiLLyA HashMap would make much more since... but heres one way to write the function asked for. Assuming case-insensitive (a=A). Also does not have error handling or non-alpha strings
public List groupByFirstLetter(List words){
List groups = new ArrayList();
//initial all buckets
for(int i = 0; i < 26; i++{
List l = new ArrayList();
groups.add(l);
}
//add words to group
for(String word : words){
char firstLetter = word..toLowerCase().charAt(0);
Integer index = ((int) firstLetter) - 97; //unicode int value of 'a'
//might want to do some try catch here...
groups.get(index).add(word);
}
}
I wrote this complete in the thread reply so its likely there are some compile errors. Should get you started though.