Here you can find the source of getSubListIndex(Object[] tofind, Object[] tokens)
Parameter | Description |
---|---|
tofind | array you want to find in tokens |
tokens | a parameter |
public static List<Integer> getSubListIndex(Object[] tofind, Object[] tokens)
//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; } }