Computer Science Canada Please Help, should be working but isn't |
Author: | EricUnreal [ Thu Jan 26, 2012 11:19 pm ] |
Post subject: | Please Help, should be working but isn't |
My final is tommorrow and i'm having trouble with some questions that are similar to those that will be on the exam. Here's what i have: I have to insert a word into the list while maintaining alphabetical order. What i'm trying to do is insert the word, then just re-sort the list. import java.util.*; import java.io.*; public class WordList { private ArrayList<String> list; public WordList() { list = new ArrayList<String>(); getData(); } public void addWord(String aWord) { list.add(aWord); for (int current=0; current<list.length; current++) { int lowPos = current; for(int i=current; i<list.length; i++) { if( list[i].compareTo(list[lowPos])<0 ) { lowPos = i; } } String temp = list[current]; list[current] = list[lowPos]; list[lowPos] = temp; } } } |
Author: | EricUnreal [ Thu Jan 26, 2012 11:25 pm ] |
Post subject: | RE:Please Help, should be working but isn\'t |
To clarify, it's giving me an error at list.length; which i dont think it should because it is an ArrayList |
Author: | Tony [ Thu Jan 26, 2012 11:49 pm ] |
Post subject: | RE:Please Help, should be working but isn\'t |
there is no such field in ArrayList |
Author: | EricUnreal [ Thu Jan 26, 2012 11:52 pm ] |
Post subject: | Re: RE:Please Help, should be working but isn\'t |
Tony @ Thu Jan 26, 2012 10:49 pm wrote: there is no such field in ArrayList
Well what could I use instead? or even in an Array. |
Author: | Tony [ Fri Jan 27, 2012 12:23 am ] |
Post subject: | RE:Please Help, should be working but isn\'t |
Perhaps you'll find one of the available methods to be suitable? http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html |