Java Array Contain arrayContains1(String[] parent, String[] child)

Here you can find the source of arrayContains1(String[] parent, String[] child)

Description

test if all items in parent also appears in child

License

Apache License

Parameter

Parameter Description
parent a parameter
child a parameter

Declaration

private static boolean arrayContains1(String[] parent, String[] child) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    /**//from  ww w.  j av  a  2  s  . c o  m
     * test if all items in parent also appears in child
     * 
     * @param parent
     * @param child
     * @return
     */
    private static boolean arrayContains1(String[] parent, String[] child) {
        if (child.length == 0) {
            return true;
        }

        String[] parentCopy = Arrays.copyOf(parent, parent.length);
        String[] childCopy = Arrays.copyOf(child, child.length);

        Arrays.sort(parentCopy);
        Arrays.sort(childCopy);

        int i = 0, j = 0;

        while (i < parentCopy.length && j < childCopy.length) {
            if (childCopy[j].equals(parentCopy[i])) {
                i++;
                j++;
            } else {
                j++;
            }
        }

        if (i == parentCopy.length) {
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. arrayContains(T[] arr, T item)
  2. arrayContains(T[] array, T value)
  3. arrayContains(T[] source, T[] target)
  4. arrayContains(T[] src, T target)
  5. arrayContains(T[] ts, T t)
  6. arrayContainsElement(T[] array, T element)
  7. arrayContainsEntry(T[] array, T entry)
  8. arrayContainsIgnoreCase(String[] array, String searchKey)
  9. arrayContainsIgnoreCase(String[] array, String str)