Java List IndexOf indexOf(List source, List target)

Here you can find the source of indexOf(List source, List target)

Description

index Of

License

Apache License

Declaration

public static <S> int indexOf(List<S> source, List<S> target) 

Method Source Code


//package com.java2s;
/*/*  www.j  av a2  s  .  c  o m*/
 * Copyright  2013  Ali Ok (aliokATapacheDOTorg)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.util.*;

public class Main {
    public static <S> int indexOf(List<S> source, List<S> target) {
        return indexOf(source, target, 0);
    }

    public static <S> int indexOf(List<S> source, List<S> target, int fromIndex) {
        return indexOf(source, 0, source.size(), target, 0, target.size(), fromIndex);
    }

    public static <S> int indexOf(List<S> source, int sourceOffset, int sourceCount, List<S> target,
            int targetOffset, int targetCount, int fromIndex) {
        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        S first = target.get(targetOffset);
        int max = sourceOffset + (sourceCount - targetCount);

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (!source.get(i).equals(first)) {
                while (++i <= max && !source.get(i).equals(first))
                    ;
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = targetOffset + 1; j < end && source.get(j).equals(target.get(k)); j++, k++)
                    ;

                if (j == end) {
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;
    }
}

Related

  1. indexOf(final String regex, final List list)
  2. indexOf(int hash, List list)
  3. indexOf(int start, List datas, T target)
  4. indexOf(List list, Object element, int begin, int end)
  5. indexOf(List data, List token)
  6. indexOf(List lines, String... conditions)
  7. indexOf(List list, T string, int beginIndex)
  8. indexOf(Object o, Collection list)
  9. indexOfFactory(List factories, Class classInstance)