I have a project in my comp 182 class we're working on vectors but I'm stuck on making an "ordered vector". When I try to run it I get an ArrayOutofBounds error.
(The "howMany" variable is the count for the size of the Strings in the array "theWords"
And the code is run from another class that reads an input file with 10 words in it,using this "addWord" method to add the words from the file into the "theWords" array.)
Here's the code I have so far:
[btw we're not allowed to use the "Array" methods only "compareTo"]
public void addWord(String newWord) {
//adds in words
if (howMany < theWords.length) {
theWords[howMany]= newWord;
howMany++;
}
else {
String t [] = new String[capacity+10];
for (int i=0; i <capacity; i++){
t[i] = theWords[i];
}
theWords = t;
theWords[howMany] = newWord;
howMany++;
}
//ordering words
for(int g = howMany - 1, z = howMany ; g < howMany; g--, z--) {
if(newWord.compareTo(theWords[g]) < 0) {
theWords[g] = theWords[z];
theWords[g] = newWord;
}
else
newWord = theWords[z];
}
howMany++;
}
Any help is much appreciated!