Java tutorial
//package com.java2s; /* * Copyright 2011-2013 HTTL Team. * * 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.lang.reflect.Array; public class Main { public static boolean[] subArray(boolean[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return new boolean[0]; } int len = array.length; boolean[] sub = new boolean[indexs.length]; for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } public static char[] subArray(char[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return new char[0]; } int len = array.length; char[] sub = new char[indexs.length]; for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } public static byte[] subArray(byte[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return new byte[0]; } int len = array.length; byte[] sub = new byte[indexs.length]; for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } public static short[] subArray(short[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return new short[0]; } int len = array.length; short[] sub = new short[indexs.length]; for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } public static int[] subArray(int[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return new int[0]; } int len = array.length; int[] sub = new int[indexs.length]; for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } public static long[] subArray(long[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return new long[0]; } int len = array.length; long[] sub = new long[indexs.length]; for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } public static float[] subArray(float[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return new float[0]; } int len = array.length; float[] sub = new float[indexs.length]; for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } public static double[] subArray(double[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return new double[0]; } int len = array.length; double[] sub = new double[indexs.length]; for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } @SuppressWarnings("unchecked") public static <T> T[] subArray(T[] array, int[] indexs) { if (array == null || array.length == 0) { return array; } if (indexs == null || indexs.length == 0) { return (T[]) Array.newInstance(array.getClass().getComponentType(), 0); } int len = array.length; T[] sub = (T[]) Array.newInstance(array.getClass().getComponentType(), indexs.length); for (int i = 0; i < indexs.length; i++) { int index = indexs[i]; if (index < 0) { index = len + index; } if (index >= 0 && index < len) { sub[i] = array[index]; } } return sub; } }