Java Utililty Methods Array Swap

List of utility methods to do Array Swap

Description

The list of methods to do Array Swap are organized into topic(s).

Method

voidswapArray(int[] v, int i, int j)
Swap elements in list
int t = v[i];
v[i] = v[j];
v[j] = t;
String[]swapArrayStr(String[] arr)
swap Array Str
String[] ret = new String[arr.length];
for (int i = 1; i < arr.length; i++) {
    ret[i - 1] = arr[i].trim();
ret[arr.length - 1] = arr[0];
return ret;
voidswapArrayValues(int i1, int i2, int[] indices)
Swaps the two indexed values in the indices array.
int temp = indices[i1];
indices[i1] = indices[i2];
indices[i2] = temp;
voidswapBlocks(byte[] array, int fromA, int toA, int fromB, int toB)
Swaps two non intersecting blocks inside an array of bytes.
checkArrayBlocksRanges(fromA, toA, fromB, toB, array.length);
final int lFrom = Math.min(fromA, fromB);
final int lTo = Math.min(toA, toB);
final int rFrom = Math.max(fromA, fromB);
final int rTo = Math.max(toA, toB);
reverseBytes(array, lFrom, lTo); 
reverseBytes(array, lTo, rFrom); 
reverseBytes(array, rFrom, rTo); 
...
byte[]swapByteArray(byte[] source)
Swaps the given byte array order.
if (source == null)
    throw new NullPointerException("Source cannot be null.");
if (source.length == 0)
    return new byte[0];
byte[] swapped = new byte[source.length];
for (int i = 0; i < source.length; i++)
    swapped[source.length - i - 1] = source[i];
return swapped;
...
voidswapByteOrder(byte[] a)
swap Byte Order
for (int i = 0; i < a.length / 2; i++) {
    byte tmp = a[i];
    a[i] = a[a.length - 1 - i];
    a[a.length - 1 - i] = tmp;
byte[]swapByteOrder32(byte[] data, int ofs, int len)
swap Byte Order
int end = ofs + len;
byte tmp;
for (; ofs < end; ofs += 4) {
    tmp = data[ofs];
    data[ofs] = data[ofs + 3];
    data[ofs + 3] = tmp;
    tmp = data[ofs + 1];
    data[ofs + 1] = data[ofs + 2];
...
byte[]swapBytes(byte[] bytes)
swap by bytes
if (bytes == null || bytes.length < 2)
    return null;
byte[] nbytes = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
    nbytes[i] = bytes[bytes.length - 1 - i];
return nbytes;
byte[]swapBytes(byte[] data)
swap Bytes
int totalLength = data.length;
byte[] buffer = new byte[totalLength];
for (int i = 0; i < totalLength; i++)
    buffer[i] = data[totalLength - 1 - i];
return buffer;
byte[]swapBytes(byte[] dataToSwap, int wordByteLength)
Return an array which contains the same data as dataToSwap but with byte reversed.
byte[] result = null;
if (dataToSwap != null) {
    if (wordByteLength < 1 || dataToSwap.length % wordByteLength > 0) {
        throw new IndexOutOfBoundsException(
                "The wordByteLength is not a multiple of input data. wordByteLength=" + wordByteLength
                        + ", inputDataSize=" + dataToSwap.length);
    result = new byte[dataToSwap.length];
...