I need to check the array to see if the user input is already present, and display a message as to whether it is or isn\'t there. The first part is working, but I tried to creat
One simple solution is to use a Set
:
Set words = new HashSet();
Add words with the add()
method and check if a word is already added with contains(word)
method.
EDIT
If you must use Arrays
you can keep the array sorted and do a binary search:
Arrays.sort(words);
boolean isAlreadyAdded = Arrays.binarySearch(words, newWord) >= 0;