Java Array Intersect hasIntersection(String a1[], String a2[], int mode)

Here you can find the source of hasIntersection(String a1[], String a2[], int mode)

Description

has Intersection

License

Apache License

Declaration

static public boolean hasIntersection(String a1[], String a2[], int mode) 

Method Source Code

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

import java.util.ArrayList;
import java.util.Arrays;

public class Main {

    static public boolean hasIntersection(String a1[], String a2[], int mode) {
        if (a1 == null || a2 == null)
            return false;
        if (a1 == a2)
            return a1.length > 0;
        java.util.List<String> v = new ArrayList<String>();
        createIntersection(a1, a2, mode, v, false);
        return v.size() > 0;
    }/*from w ww.  ja va  2s . com*/

    static public String[] createIntersection(String a1[], String a2[], int mode) {
        if (a1 == null || a2 == null)
            return null;
        if (a1 == a2)
            return a1;
        final java.util.List<String> v = new java.util.ArrayList<String>();
        createIntersection(a1, a2, mode, v, false);
        return v.toArray(new String[v.size()]);
    }

    static final private void createIntersection(String a1[], String a2[], int mode, java.util.List<String> v,
            boolean forHas) {
        a1 = (String[]) a1.clone();
        Arrays.sort(a1);// ,sc);
        a2 = (String[]) a2.clone();
        Arrays.sort(a2);// ,sc);
        for (int i = 0; i < a1.length; i++) {
            String s = a1[i];
            if (s != null) {
                int j = Arrays.binarySearch(a2, s);// ,sc);
                if (j < 0 && mode == 1) {
                    for (int l = s.length() - 1; l > 0; l--)
                        if ((j = Arrays.binarySearch(a2, s.substring(0, l))) >= 0)
                            break;
                }
                if (j >= 0) {
                    v.add(s);
                    if (forHas)
                        return;
                }
            }
        }
        if (mode == 1)
            for (int i = 0; i < a2.length; i++) {
                String s = a2[i];
                if (s == null)
                    continue;
                int j = -1;
                for (int l = s.length() - 1; l > 0; l--)
                    if ((j = Arrays.binarySearch(a1, s.substring(0, l))) >= 0)
                        break;
                if (j >= 0) {
                    v.add(s);
                    if (forHas)
                        return;
                }
            }
    }

    public static final String subString(String str, char delimiter, int index) {
        return subSplitString(str, 0, delimiter, index);
    }

    public static final String subSplitString(String str, int istart, char delimiter, int index) {
        if (str == null)
            return null;
        int sl = str.length();
        int i = istart, j = 0;
        for (; i < sl;) {
            int iend = str.indexOf(delimiter, i);
            if (iend < 0)
                break;
            if (j++ == index)
                return str.substring(i, iend);
            // System.out.println(sa[j-1]);
            i = iend + 1;
        }
        return j == index ? str.substring(i) : null;
    }

    public static final String subSplitString(String str, char delimiter, int index) {
        return subSplitString(str, 0, delimiter, index);
    }
}

Related

  1. arrayInterp(double[] inputArray, double index)
  2. arraysIntersect(Object[] array1, Object[] array2)
  3. byteIntersection(byte[] a, byte[] b)
  4. getArrayIntersection(int a[], int b[])
  5. getNonIntersection(int[] interval, int[] intervalToRemove)
  6. intersect(boolean[] mask, int[] examples, boolean[] intersection)
  7. intersect(int[] sorted1, int[] sorted2)
  8. intersect(int[]... arrays)
  9. intersect(String[] arr1, String[] arr2)