Java Array Search searchFirst(String[] target, String key)

Here you can find the source of searchFirst(String[] target, String key)

Description

search First

License

Open Source License

Declaration

public static int searchFirst(String[] target, String key) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

import java.util.List;

public class Main {
    public static int searchFirst(String[] target, String key) {
        if (isEmpty(target) || key == null) {
            return -1;
        }// ww w.j  a  v  a  2s  . c o  m
        for (int i = 0; i < target.length; i++) {
            if (key.equals(target[i])) {
                return i;
            }
        }
        return -1;
    }

    public static boolean isEmpty(String[] arg) {
        if (arg == null) {
            return true;
        }
        return arg.length == 0;
    }

    public static boolean equals(String[] fullSet, String[] subSet) {
        if (isEmpty(fullSet) && isEmpty(subSet)) {
            return true;
        }

        if (length(fullSet) != length(subSet)) {
            return false;
        }

        List<String> fullList = Arrays.asList(fullSet);
        List<String> subList = Arrays.asList(subSet);
        return fullList.containsAll(subList) && subList.containsAll(fullList);
    }

    public static int length(String[] strs) {
        if (isEmpty(strs)) {
            return 0;
        }
        return strs.length;
    }
}

Related

  1. memchr(char[] haystack, int offset, char needle, int num)
  2. memchr(T[] ptr, T value)
  3. replaceAll(String text, char[] toSearch, char toReplace)
  4. search(double[] input, int[] bounds, double x)
  5. Search(int[] in, int val)
  6. searchInStringArray(String key, String[] data)