Java Array Swap swapValues(double[] arr, double v1, double v2)

Here you can find the source of swapValues(double[] arr, double v1, double v2)

Description

This changes all occurrences of the value v1 to v2 and vice versa.

License

Open Source License

Parameter

Parameter Description
arr the array to be processed
v1 the value to be changed to v2
v2 the value to be change to v1

Declaration

public static void swapValues(double[] arr, double v1, double v2) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w ww . j ava 2 s  .c o  m
     * This changes all occurrences of the value v1 to v2 and vice versa. Other data in the array
     * are left unchanged.
     * @param arr the array to be processed
     * @param v1 the value to be changed to v2
     * @param v2 the value to be change to v1
     */
    public static void swapValues(double[] arr, double v1, double v2) {

        for (int i = 0; i < arr.length; ++i) {
            if (arr[i] == v1) {
                arr[i] = v2;
            } else if (arr[i] == v2) {
                arr[i] = v1;
            }
        }

    }
}

Related

  1. swapParallel(Object[] x, Object[] y, int a, int b, int yrel)
  2. swapPerTowElement(int[] arr)
  3. swapReferences(Object[] a, int index1, int index2)
  4. swapRgbColorComponents(byte[] pixelArray)
  5. swapTokens(String grid[])
  6. swapZeros(int[] input, int zeroCount)