Revision: 51701
Updated Code
at October 1, 2011 16:50 by Cur3n4
Updated Code
public static <T> int indexOfSubArray(T[] array, T[] subarray){ if (array == null || subarray == null) return -1; List<T> list = Arrays.asList(array); List<T> subList = Arrays.asList(subarray); return Collections.indexOfSubList(list, sublist); } public static int indexOfSubArray(int[] array, int[] subarray) { return indexOfSubArray(toObjectArray(array), toObjectArray(subarray)); } // Alternatively use commons lang ArrayUtils to convert it into an array of objects private static Integer[] toObjectArray(int[] array) { if (array == null) return null; Integer[] result = new Integer[array.length]; for (int i=0; i<array.length; i++) result[i] = new Integer(array[i]); return result; }
Revision: 51700
Updated Code
at October 1, 2011 16:38 by Cur3n4
Updated Code
public static <T> int indexOfSubArray(T[] array, T[] subarray){ if (array == null || subarray == null) return -1; List<T> list = Arrays.asList(array); List<T> subList = Arrays.asList(subarray); return Collections.indexOfSubList(list, sublist); } public static int indexOfSubArray(int[] array, int[] subarray) { if (array == null || subarray == null) return -1; List<T> list = toObjectList(array); List<T> subList = toObjectList(subarray); return Collections.indexOfSubList(list, sublist); } // Alternatively use commons lang ArrayUtils to convert it into an array of objects private static List<Integer> toObjectList(int[] array) { if (array == null) return null; List<Integer> result = new ArrayList<Integer>(array.length); for (int i=0; i<array.length; i++) result.add(new Integer(array[i])); return result; }
Revision: 51699
Updated Code
at October 1, 2011 16:01 by Cur3n4
Updated Code
public static <T> int indexOfSubArray(T[] array, T[] subarray){ if (array == null || subarray == null) return -1; List<T> list = Arrays.asList(array); List<T> subList = Arrays.asList(subarray); return Collections.indexOfSubList(list, sublist); } public static int indexOfSubArray(int[] array, int[] subarray) { return indexOfSubArray(toObjectArray(array), toObjectArray(subarray)); } // Alternatively use commons lang ArrayUtils private static Integer[] toObjectArray(int[] array) { if (array == null) return null; Integer[] result = new Integer[array.length]; for (int i=0; i<array.length; i++) result[i] = new Integer(array[i]); return result; }
Revision: 51698
Updated Code
at October 1, 2011 15:57 by Cur3n4
Updated Code
public static <T> int indexOfSubAaray(T[] array, T[] subarray){ List<T> list = Arrays.asList(array); List<T> subList = Arrays.asList(subarray); return Collections.indexOfSubList(list, sublist); } public static int indexOfSubArray(int[] array, int[] subarray) { } // Alternatively use commons lang ArrayUtils private static Integer[] toObjectArray(int[] array) { if (array == null) return null; Integer[] result = new Integer[array.length]; for (int i=0; i<array.length; i++) result[i] = new Integer(array[i]); return result; }
Revision: 51697
Updated Code
at October 1, 2011 15:50 by Cur3n4
Updated Code
public static <T> int indexOfSubAaray(T[] array, T[] subarray){ List<T> list = Arrays.asList(array); List<T> subList = Arrays.asList(subarray); return Collections.indexOfSubList(list, sublist); } public static int indexOfSubArray(int[] array, int[] subarray) { } // Alternatively use commons lang ArrayUtils private static Integer[] toObjectArray(int[] array) { if (array == null) return null; Integer[] result = new Integer[array.length]; for (int i=0; i<array.length; i++) result[i] = new Integer(array[i]);
Revision: 51696
Updated Code
at October 1, 2011 15:37 by Cur3n4
Updated Code
public static <T> int indexOfSubAaray(T[] array, T[] subarray){ List<T> list = Arrays.asList(array); List<T> subList = Arrays.asList(subarray); return Collections.indexOfSubList(list, sublist); }
Revision: 51695
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 1, 2011 14:17 by Cur3n4
Initial Code
public int a(){}
Initial URL
Initial Description
I assume that the arrays have to be of the same type. I have decided to use the method indexOfSubList provided by the Collections class as a way of simplifying our code. This method does exactly what is required, but for lists. The main issue with the method is that it doesn't support arrays of primitives, so we need to create additional methods for each primitive (int, long, double, etc) where the array of primitives gets transformed into an array of objects. In the example below only int is implemented. This might have a performance impact on some scenarios because of the conversion of arrays to lists, I suppose the impact is mainly for arrays of primitives. However the code remains simpler, and probably the implementation of the indexOfSubList method in the Collections class is quite efficient. Some unit tests should be developed to test if the performance is acceptable. As we can see the code of indexOfSubArray methods below is quite concise.
Initial Title
Answer question 2
Initial Tags
Initial Language
Java