Here you can find the source of getFoundIdx(char to_find, char[] to_search, boolean do_orderAsc)
Get the (first) index at which a char exists in a char-array.
Parameter | Description |
---|---|
to_find | The char to analyze. |
to_search | The array which to_find should be in. May not be null, and, if do_orderAsc is true, <i>should</i> be non-empty, unique, and sorted ascending. If not, this function will not work properly. |
do_orderAsc | If true, then <code> java.util.Arrays . java.util.Arrays#binarySearch(char[],char) binarySearch (c[],c)</code> is used to search the array. If false, a for loop is used. |
public static final int getFoundIdx(char to_find, char[] to_search, boolean do_orderAsc)
//package com.java2s; /*license*\/*from w w w.j a v a2 s. c o m*/ XBN-Java: Copyright (C) 2014, Jeff Epstein (aliteralmind __DASH__ github __AT__ yahoo __DOT__ com) This software is dual-licensed under the: - Lesser General Public License (LGPL) version 3.0 or, at your option, any later version; - Apache Software License (ASL) version 2.0. Either license may be applied at your discretion. More information may be found at - http://en.wikipedia.org/wiki/Multi-licensing. The text of both licenses is available in the root directory of this project, under the names "LICENSE_lgpl-3.0.txt" and "LICENSE_asl-2.0.txt". The latest copies may be downloaded at: - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt \*license*/ import java.util.Arrays; public class Main { /** <p>Get the (first) index at which a char exists in a char-array.</p> * @return {@link #getFoundIdx(char, char[], boolean) getFoundIdx(to_find, to_search, true)} */ public static final int getFoundIdx(char to_find, char[] to_search) { return getFoundIdx(to_find, to_search, true); } /** <p>Get the (first) index at which a char exists in a char-array.</p> * @param to_find The char to analyze. * @param to_search The array which {@code to_find} should be in. May not be null, and, if {@code do_orderAsc} is true, <i>should</i> be non-empty, unique, and sorted ascending. If not, this function will not work properly. * @param do_orderAsc If true, then <code>{@link java.util.Arrays}.{@link java.util.Arrays#binarySearch(char[], char) binarySearch}(c[],c)</code> is used to search the array. If false, a for loop is used. * @return The first index in {@code to_search} at which {@code to_find} exists. <br/>{@code <b>-1</b>} If it doesn't. */ public static final int getFoundIdx(char to_find, char[] to_search, boolean do_orderAsc) { if (to_search == null) { throw new NullPointerException("to_search"); } if (do_orderAsc) { return Arrays.binarySearch(to_search, to_find); } for (int i = 0; i < to_search.length; i++) { if (to_find == to_search[i]) { return i; } } return -1; } }