Java - Write code to Return true if a string array contains a given string.

Requirements

Write code to Return true if a string array contains a given string.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String[] list = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        String item = "book2s.com";
        System.out.println(containsString(list, item));
    }//from  w w w  .  ja v  a2 s.  c o m

    /**
     * Return true if a string array contains a given string.
     */
    public static boolean containsString(String[] list, String item) {
        if (item == null)
            throw new IllegalArgumentException("Argument 'item' is null");

        for (int i = 0; i < list.length; i++) {
            if (item.equals(list[i]))
                return true;
        }

        return false;
    }
}