Java List Sub List getSubListIndex(Object[] tofind, Object[] tokens)

Here you can find the source of getSubListIndex(Object[] tofind, Object[] tokens)

Description

If tofind is a part of tokens, it finds the starting index of tofind in tokens If tofind is not a sub-array of tokens, then it returns null note that tokens sublist should have the exact elements and order as in tofind

License

Open Source License

Parameter

Parameter Description
tofind array you want to find in tokens
tokens a parameter

Return

starting index of the sublist

Declaration

public static List<Integer> getSubListIndex(Object[] tofind,
        Object[] tokens) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**// w ww . j  a va  2s .com
     * If tofind is a part of tokens, it finds the starting index of tofind in tokens
     * If tofind is not a sub-array of tokens, then it returns null
     * note that tokens sublist should have the exact elements and order as in tofind
     * @param tofind array you want to find in tokens
     * @param tokens
     * @return starting index of the sublist
     */
    public static List<Integer> getSubListIndex(Object[] tofind,
            Object[] tokens) {
        if (tofind.length > tokens.length)
            return null;
        List<Integer> allIndices = new ArrayList<Integer>();
        boolean matched = false;
        int index = -1;
        int lastUnmatchedIndex = 0;
        for (int i = 0; i < tokens.length;) {
            for (int j = 0; j < tofind.length;) {
                if (tofind[j].equals(tokens[i])) {
                    index = i;
                    i++;
                    j++;
                    if (j == tofind.length) {
                        matched = true;
                        break;
                    }
                } else {
                    j = 0;
                    i = lastUnmatchedIndex + 1;
                    lastUnmatchedIndex = i;
                    index = -1;
                    if (lastUnmatchedIndex == tokens.length)
                        break;
                }
                if (i >= tokens.length) {
                    index = -1;
                    break;
                }
            }
            if (i == tokens.length || matched) {
                if (index >= 0)
                    //index = index - l1.length + 1;
                    allIndices.add(index - tofind.length + 1);
                matched = false;
                lastUnmatchedIndex = index;

                //break;
            }
        }
        //get starting point

        return allIndices;
    }

    /**
     * Tests two double[][] arrays for having equal contents.
     * @return true iff for each i, <code>equals(xs[i],ys[i])</code> is true
     */
    public static boolean equals(double[][] xs, double[][] ys) {
        if (xs == null)
            return ys == null;
        if (ys == null)
            return false;
        if (xs.length != ys.length)
            return false;
        for (int i = xs.length - 1; i >= 0; i--) {
            if (!Arrays.equals(xs[i], ys[i]))
                return false;
        }
        return true;
    }

    /**
     * Tests two boolean[][] arrays for having equal contents.
     * @return true iff for each i, <code>Arrays.equals(xs[i],ys[i])</code> is true
     */
    @SuppressWarnings("null")
    public static boolean equals(boolean[][] xs, boolean[][] ys) {
        if (xs == null && ys != null)
            return false;
        if (ys == null)
            return false;
        if (xs.length != ys.length)
            return false;
        for (int i = xs.length - 1; i >= 0; i--) {
            if (!Arrays.equals(xs[i], ys[i]))
                return false;
        }
        return true;
    }
}

Related

  1. getSubList(List list, int from, int maxnum)
  2. getSublist(List list, int i)
  3. getSubList(List list, int start, int end)
  4. getSubList(List list, int start, int limit)
  5. getSubListFromStart(List list, int length)
  6. isSubList(List l1, List l)
  7. lastIndexOfSubList(final List list0, final List list1)
  8. slice(List stringList, int subListSize)
  9. split(List from, int subListSize)