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

voidswapPerTowElement(int[] arr)
swap Per Tow Element
int len = arr.length;
for (int i = 1; i < len; i += 2) {
    swap(arr, i, i - 1);
voidswapReferences(Object[] a, int index1, int index2)
Method to swap to elements in an array.
Object tmp = a[index1];
a[index1] = a[index2];
a[index2] = tmp;
voidswapRgbColorComponents(byte[] pixelArray)
Swaps every R channel with B channel for every pixel in passed array thus converting beetwen RGB and BGR.
if (pixelArray == null)
    throw new IllegalArgumentException("pixelArray cannot be null.");
for (int i = 0; i < pixelArray.length; i += 3) {
    byte temp = pixelArray[i];
    pixelArray[i] = pixelArray[i + 2];
    pixelArray[i + 2] = temp;
String[]swapTokens(String grid[])
swap Tokens
String stringBoard[] = new String[grid.length];
for (int i = 0; i < grid.length; i++) {
    stringBoard[i] = grid[i].replace('Y', 'R').replace('R', 'Y');
return stringBoard;
voidswapValues(double[] arr, double v1, double v2)
This changes all occurrences of the value v1 to v2 and vice versa.
for (int i = 0; i < arr.length; ++i) {
    if (arr[i] == v1) {
        arr[i] = v2;
    } else if (arr[i] == v2) {
        arr[i] = v1;
voidswapZeros(int[] input, int zeroCount)
swap Zeros
int inputLength = input.length;
int nonZeroLength = inputLength - zeroCount;
for (int i = 0; i < nonZeroLength; i++) {
    if (inputLength <= nonZeroLength) {
        break;
    int element = input[i];
    if (element == 0) {
...