Java Array Equal equalsIgnoreSequence(final T[] array1, final T[] array2)

Here you can find the source of equalsIgnoreSequence(final T[] array1, final T[] array2)

Description

equals Ignore Sequence

License

Apache License

Declaration

public static <T> boolean equalsIgnoreSequence(final T[] array1, final T[] array2) 

Method Source Code

//package com.java2s;
/*/*from   w ww .  j  a va 2s .c  o  m*/
 * Copyright 2004-2012 the Seasar Foundation and the Others.
 *
 * 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.Arrays;

public class Main {

    public static <T> boolean equalsIgnoreSequence(final T[] array1, final T[] array2) {
        if (array1 == null && array2 == null) {
            return true;
        } else if (array1 == null || array2 == null) {
            return false;
        }
        if (array1.length != array2.length) {
            return false;
        }
        final T[] copyOfArray2 = Arrays.copyOf(array2, array2.length);
        for (int i = 0; i < array1.length; i++) {
            final T o1 = array1[i];
            final int j = indexOf(copyOfArray2, o1, i);
            if (j == -1) {
                return false;
            }
            if (i != j) {
                final T o2 = copyOfArray2[i];
                copyOfArray2[i] = array2[j];
                copyOfArray2[j] = o2;
            }
        }
        return true;
    }

    public static <T> int indexOf(final T[] array, final T obj) {
        return indexOf(array, obj, 0);
    }

    public static <T> int indexOf(final T[] array, final T obj, final int fromIndex) {
        if (array != null) {
            for (int i = fromIndex; i < array.length; ++i) {
                final Object o = array[i];
                if (o != null) {
                    if (o.equals(obj)) {
                        return i;
                    }
                } else if (obj == null) {
                    return i;

                }
            }
        }
        return -1;
    }

    public static int indexOf(final boolean[] array, final boolean value) {
        if (array != null) {
            for (int i = 0; i < array.length; ++i) {
                if (array[i] == value) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static int indexOf(final byte[] array, final byte value) {
        if (array != null) {
            for (int i = 0; i < array.length; ++i) {
                if (array[i] == value) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static int indexOf(final short[] array, final short value) {
        if (array != null) {
            for (int i = 0; i < array.length; ++i) {
                if (array[i] == value) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static int indexOf(final int[] array, final int value) {
        if (array != null) {
            for (int i = 0; i < array.length; ++i) {
                if (array[i] == value) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static int indexOf(final long[] array, final long value) {
        if (array != null) {
            for (int i = 0; i < array.length; ++i) {
                if (array[i] == value) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static int indexOf(final float[] array, final float value) {
        if (array != null) {
            for (int i = 0; i < array.length; ++i) {
                if (array[i] == value) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static int indexOf(final double[] array, final double value) {
        if (array != null) {
            for (int i = 0; i < array.length; ++i) {
                if (array[i] == value) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static int indexOf(final char[] array, final char value) {
        if (array != null) {
            for (int i = 0; i < array.length; ++i) {
                if (array[i] == value) {
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

  1. equals(Object[] objs1, Object[] objs2)
  2. equals(String[] fullSet, String[] subSet)
  3. equals(T[] array1, T[] array2)
  4. equals3(double[][][] arr1, double[][][] arr2)
  5. equalsAny(T element, T[] array)
  6. equalsIgnoreSequence(Object[] array1, Object[] array2)
  7. equalsToElement(final Object[] arr, final Object obj)
  8. equalStringArrays(String[] expected, String[] actual)
  9. findMinimaGreaterOrEqual(final int[] min, final int elt)