Here you can find the source of reverse(T[] array)
Parameter | Description |
---|---|
T | a parameter |
array | a parameter |
public static <T> T[] reverse(T[] array)
//package com.java2s; /**//w w w .j av a 2 s. co m * * Copyright 2017 Florian Erhard * * 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.Collection; public class Main { public static <T> void reverse(Collection<T> p) { Object[] a = p.toArray(); reverse(a); p.clear(); for (Object e : a) p.add((T) e); } public static void reverse(double[] p) { double tmp; int n = p.length - 1; for (int i = 0; i < p.length / 2; i++) { tmp = p[i]; p[i] = p[n - i]; p[n - i] = tmp; } } public static void reverse(short[] p) { short tmp; int n = p.length - 1; for (int i = 0; i < p.length / 2; i++) { tmp = p[i]; p[i] = p[n - i]; p[n - i] = tmp; } } public static void reverse(long[] p) { long tmp; int n = p.length - 1; for (int i = 0; i < p.length / 2; i++) { tmp = p[i]; p[i] = p[n - i]; p[n - i] = tmp; } } public static void reverse(char[] p) { char tmp; int n = p.length - 1; for (int i = 0; i < p.length / 2; i++) { tmp = p[i]; p[i] = p[n - i]; p[n - i] = tmp; } } public static void reverse(float[] p) { float tmp; int n = p.length - 1; for (int i = 0; i < p.length / 2; i++) { tmp = p[i]; p[i] = p[n - i]; p[n - i] = tmp; } } /** * from inclusice, to exclusive * @param p * @param from * @param to */ public static void reverse(double[] p, int from, int to) { double tmp; int l = to - 1; int k = from; for (; k < l; k++, l--) { tmp = p[k]; p[k] = p[l]; p[l] = tmp; } } /** * from inclusice, to exclusive * @param p * @param from * @param to */ public static void reverse(int[] p, int from, int to) { int tmp; int l = to - 1; int k = from; for (; k < l; k++, l--) { tmp = p[k]; p[k] = p[l]; p[l] = tmp; } } /** * from inclusice, to exclusive * @param p * @param from * @param to */ public static void reverse(long[] p, int from, int to) { long tmp; int l = to - 1; int k = from; for (; k < l; k++, l--) { tmp = p[k]; p[k] = p[l]; p[l] = tmp; } } /** * from inclusice, to exclusive * @param p * @param from * @param to */ public static void reverse(char[] p, int from, int to) { char tmp; int l = to - 1; int k = from; for (; k < l; k++, l--) { tmp = p[k]; p[k] = p[l]; p[l] = tmp; } } /** * from inclusice, to exclusive * @param p * @param from * @param to */ public static void reverse(float[] p, int from, int to) { float tmp; int l = to - 1; int k = from; for (; k < l; k++, l--) { tmp = p[k]; p[k] = p[l]; p[l] = tmp; } } /** * Reverses the elements of an array (inplace!) * @param <T> * @param array * @return the given array for chaining */ public static <T> T[] reverse(T[] array) { for (int i = 0; i < array.length / 2; i++) { T s = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = s; } return array; } /** * Reverses the elements of an int array * @param array */ public static void reverse(int[] array) { for (int i = 0; i < array.length / 2; i++) { int s = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = s; } } public static int[] add(int[] total, int[] add) { if (total == null || total.length != add.length) total = new int[add.length]; for (int i = 0; i < total.length; i++) total[i] += add[i]; return total; } public static long[] add(long[] total, long[] add) { if (total == null || total.length != add.length) total = new long[add.length]; for (int i = 0; i < total.length; i++) total[i] += add[i]; return total; } public static double[] add(double[] total, double[] add) { if (total == null || total.length != add.length) total = new double[add.length]; for (int i = 0; i < total.length; i++) total[i] += add[i]; return total; } public static double[] add(double[] total, double add) { for (int i = 0; i < total.length; i++) total[i] += add; return total; } }