class 中的 ArrayList 相关方法,而不是程序中的 运行

ArrayList related method in a class not running in a program

我有这个程序 class 写的:

import java.util.*;

public class Dictionary {
    public static Scanner console = new Scanner(System.in);

    ///////////////////////////////////// MAIN METHOD /////////////////////////////////////
    public static void main(String[] args) {
        int choice;

        Dictionary words = new Dictionary();

        // Add some words to it
        words.addWord("dog");
        words.addWord("cat");
        words.addWord("rat");
        words.addWord("ant");
        words.addWord("pig");
        words.addWord("goat");
        words.addWord("bear");

        // Print the list of words so you can see it
        // Note that the list of words is in sorted order.
        System.out.println(words.getWords());

        do {
            choice = menu();
            switch (choice) {
                case 1: // Add
                    System.out.print("What word do you want to add? ");
                    String input = console.next();
                    System.out.println("Was the word added? " + words.addWord(input));
                    break;
                case 2: // Remove
                    System.out.print("What word do you want removed? ");
                    input = console.next();
                    System.out.println("Was the word removed? " + words.removeWord(input));
                    break;
                case 3: // Check to see if it is there
                    System.out.print("What word do you want check the location of? ");
                    input = console.next();
                    System.out.println("Was the word found in the ArrayList? " + words.isWord(input));
                    break;
                case 4: // Print
                    System.out.println(words);
                    break;
                case 5: // Find similar Words
                    System.out.print("What similarity do you want to look for? ");
                    input = console.next();
                    System.out.println("Here are the similar words: " + words.findWordsLike(input));
                    break;
                case 6: // Bye
                    System.out.println("Have a nice day!");
                    break;
                default: // huh?
                    break;
            }

        } while (choice != 6);
    }

    public static int menu() {
        int userChoice;

        System.out.println("What would you like to do?");
        System.out.println("  1.  Add a word");
        System.out.println("  2.  Remove a word");
        System.out.println("  3.  Check for the existence of a word");
        System.out.println("  4.  Print the Dictionary");
        System.out.println("  5.  Find words similar to another word");
        System.out.println("  6.  Quit");

        System.out.print("Choice>  ");
        userChoice = console.nextInt();

        return userChoice;
    }
//////////////////////////////////// Dictionary Class ////////////////////////////////        private ArrayList<String> myWords;
        private ArrayList<String> myWords;

        public Dictionary() { myWords = new ArrayList<String>(); }

        public ArrayList<String> getWords() { return myWords; }

        public void setMyWords(ArrayList<String> myWords) { this.myWords = myWords; }

        public boolean addWord(String word) {
            // Adds the variable word to myWords in the appropriate location
            // myWords is already sorted so you will need to find out where
            // word belongs and insert it there.
            // There is a method below name findWord that should
            // perform a binary search on the myWords array.
            // DO NOT RE-SORT THE ARRAY
            // Return value: true if the word was added
            //               false if not added (because already there or full)
            int index = findWord(word);
            if (index <= 0 && index != -1) {
                myWords.set((-(index + 1)), word);
                return true;
            } else {
                return false;
            }
        }

        public boolean removeWord(String word) {
            // This method will find a word in myWords and remove it.  Keep
            // in mind that myWords is sorted and you must maintain that
            // sorted order.  You will have to find the word using a binary
            // search to receive full credit (Maybe make a method call?!)
            // DO NOT RE-SORT THE myWords ARRAY
            // Return Value: true if the word was remove
            //               false if the word was not remove (not found)
            int index;
            index = Collections.binarySearch(myWords, word);
            if (index > 0)  {
                myWords.remove(index);
                return true;
            } else {
                return false;
            }
        }

        public boolean isWord(String word) {
            // Determine if the word passed to the method is in myWords
            // Must use a binary search for full credit.
            // Return Value: true if the word is in myWords
            //               false if the word is not in myWords
            int index;
            Collections.sort(myWords);
            index = Collections.binarySearch(myWords, word);
            if (index < 0) {
                return false;
            } else {
                return true;
            }
        }

        public ArrayList<String> findWordsLike(String likeMe) {
            // Looks through the entire myWords array and any word that
            // contains likeMe should be added to the ArrayList.
            // Example: likeMe = do
            //          ArrayList could contain: do, dogma, dope, redo, todo
            ArrayList<String> similarWords = new ArrayList<String>();
            for (int i = 0; i < myWords.size(); i++) {
                if (myWords.get(i).contains(likeMe)) {
                    similarWords.add(myWords.get(i));
                }
            }
            return similarWords;
        }

        private int findWord(String word) {
            // Performs a binary search on myWords to determine if word is in myWords
            // Return Value: location of word or the 2's complement of where it belongs
            // Keep in mind myWords is not full
            int low = 0;
            int high = myWords.size() - 1;

            while(high >= low) {
                int middle = (low + high) / 2;
                if (myWords.get(middle) == word) {
                    return middle;
                }
                if (!myWords.get(middle).equals(word)) {
                    if (middle < myWords.indexOf(word)) {
                        low = middle+ 1;
                    } else if (middle > myWords.indexOf(word)) {
                        high = middle - 1;
                    }
                }
            }
            return -1;
        }

    }

字典中的注释 class 描述了每个方法应该做什么。我的问题是 addWord 方法,因为当我 运行 程序时,我得到一个空的 ArrayList 并且没有添加任何内容。我可以弄清楚我哪里出错了。感谢您的帮助!

在你的addWord方法中替换

int index = findWord(word);
if (index <= 0 && index != -1) {
    myWords.set((-(index + 1)), word);

int index = myWords.indexOf(word);
if (index == -1){
    myWords.add(word);

它会起作用。