Determines whether a specified word has already been added to the array. - Java Collection Framework

Java examples for Collection Framework:Array Auto Increment

Description

Determines whether a specified word has already been added to the array.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String word = "java2s.com";
        String[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(wordAlreadyAdded(word, array));
    }//from w  ww . ja  v a  2s  . c  o m

    /**
     * Determines whether a specified word has already been added to the array.
     * 
     * @param word
     *            -- the word to search for in the array
     * @param array
     *            -- the array to be searched
     * @return whether the word has already been added to the array
     */
    private static boolean wordAlreadyAdded(String word, String[] array) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null && array[i].equalsIgnoreCase(word)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials