Here you can find the source of swap(final Object[] array, int offset1, int offset2)
Parameter | Description |
---|---|
array | the array to swap, may be null |
offset1 | the index of the first element to swap |
offset2 | the index of the second element to swap |
public static void swap(final Object[] array, int offset1, int offset2)
//package com.java2s; /*/*ww w. j ava 2 s .co m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ public class Main { /** * Swaps two elements in the given array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap(["1", "2", "3"], 0, 2) -> ["3", "2", "1"]</li> * <li>ArrayUtils.swap(["1", "2", "3"], 0, 0) -> ["1", "2", "3"]</li> * <li>ArrayUtils.swap(["1", "2", "3"], 1, 0) -> ["2", "1", "3"]</li> * <li>ArrayUtils.swap(["1", "2", "3"], 0, 5) -> ["1", "2", "3"]</li> * <li>ArrayUtils.swap(["1", "2", "3"], -1, 1) -> ["2", "1", "3"]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final Object[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps two elements in the given long array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap([true, false, true], 0, 2) -> [true, false, true]</li> * <li>ArrayUtils.swap([true, false, true], 0, 0) -> [true, false, true]</li> * <li>ArrayUtils.swap([true, false, true], 1, 0) -> [false, true, true]</li> * <li>ArrayUtils.swap([true, false, true], 0, 5) -> [true, false, true]</li> * <li>ArrayUtils.swap([true, false, true], -1, 1) -> [false, true, true]</li> * </ul> * * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final long[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps two elements in the given int array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final int[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps two elements in the given short array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final short[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps two elements in the given char array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final char[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps two elements in the given byte array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final byte[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps two elements in the given double array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final double[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps two elements in the given float array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final float[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps two elements in the given boolean array. * * <p>There is no special handling for multi-dimensional arrays. This method * does nothing for a {@code null} or empty input array or for overflow indices. * Negative indices are promoted to 0(zero).</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li> * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element to swap * @param offset2 the index of the second element to swap * @since 3.5 */ public static void swap(final boolean[] array, int offset1, int offset2) { if (array == null || array.length == 0) { return; } swap(array, offset1, offset2, 1); } /** * Swaps a series of elements in the given boolean array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap([true, false, true, false], 0, 2, 1) -> [true, false, true, false]</li> * <li>ArrayUtils.swap([true, false, true, false], 0, 0, 1) -> [true, false, true, false]</li> * <li>ArrayUtils.swap([true, false, true, false], 0, 2, 2) -> [true, false, true, false]</li> * <li>ArrayUtils.swap([true, false, true, false], -3, 2, 2) -> [true, false, true, false]</li> * <li>ArrayUtils.swap([true, false, true, false], 0, 3, 3) -> [false, false, true, true]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final boolean[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { boolean aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } /** * Swaps a series of elements in the given byte array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final byte[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { byte aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } /** * Swaps a series of elements in the given char array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final char[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { char aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } /** * Swaps a series of elements in the given double array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final double[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { double aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } /** * Swaps a series of elements in the given float array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final float[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { float aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } /** * Swaps a series of elements in the given int array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final int[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { int aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } /** * Swaps a series of elements in the given long array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final long[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { long aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } /** * Swaps a series of elements in the given array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 2, 1) -> ["3", "2", "1", "4"]</li> * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 0, 1) -> ["1", "2", "3", "4"]</li> * <li>ArrayUtils.swap(["1", "2", "3", "4"], 2, 0, 2) -> ["3", "4", "1", "2"]</li> * <li>ArrayUtils.swap(["1", "2", "3", "4"], -3, 2, 2) -> ["3", "4", "1", "2"]</li> * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 3, 3) -> ["4", "2", "3", "1"]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final Object[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { Object aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } /** * Swaps a series of elements in the given short array. * * <p>This method does nothing for a {@code null} or empty input array or * for overflow indices. Negative indices are promoted to 0(zero). If any * of the sub-arrays to swap falls outside of the given array, then the * swap is stopped at the end of the array and as many as possible elements * are swapped.</p> * * Examples: * <ul> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li> * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li> * </ul> * * @param array the array to swap, may be {@code null} * @param offset1 the index of the first element in the series to swap * @param offset2 the index of the second element in the series to swap * @param len the number of elements to swap starting with the given indices * @since 3.5 */ public static void swap(final short[] array, int offset1, int offset2, int len) { if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) { return; } if (offset1 < 0) { offset1 = 0; } if (offset2 < 0) { offset2 = 0; } if (offset1 == offset2) { return; } len = Math.min(Math.min(len, array.length - offset1), array.length - offset2); for (int i = 0; i < len; i++, offset1++, offset2++) { short aux = array[offset1]; array[offset1] = array[offset2]; array[offset2] = aux; } } }