集册 Java实例教程 使用二进制搜索检查字符串是否已在字符串[]中。

使用二进制搜索检查字符串是否已在字符串[]中。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

543
使用二进制搜索检查字符串是否已在字符串[]中。


//package com.nowjava;
/**
 from
* NowJava.com - 时代Java 
**/


public class Main {

    public static void main(String[] argv) throws Exception {

        String s1 = "nowjava.com";

        String[] arr = new String[] { "1", "abc", "level", null,

                "nowjava.com", "asdf 123" };

        System.out.println(duplicateInsertion(s1, arr));

    }


    /**

     * Uses binary searching to check if a String is already in a String[].

     * Upper and lower case words that are the same are considered duplicates.

     * ie Hat and hat are duplicates.

     * @param s1

     * @param arr

     * @return

     */

    private static boolean duplicateInsertion(String s1, String[] arr) {

        for (String s
展开阅读全文