Java Array Remove removeValues(int[] values, int[] valuesToRemove)

Here you can find the source of removeValues(int[] values, int[] valuesToRemove)

Description

removes all values in valuesToRemove array and shifts the remaining values to the left.

License

Apache License

Parameter

Parameter Description
values a parameter
valuesToRemove a parameter

Declaration

public static int[] removeValues(int[] values, int[] valuesToRemove) 

Method Source Code

//package com.java2s;
/*/*w w w.j  ava2s .  c  o m*/
 *    Copyright 2016 Roche NimbleGen Inc.
 *
 *  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.ArrayList;
import java.util.List;

public class Main {
    /**
     * removes all values in valuesToRemove array and shifts the remaining values to the left.
     * 
     * @param values
     * @param valuesToRemove
     * @return
     */
    public static int[] removeValues(int[] values, int[] valuesToRemove) {
        List<Integer> newValues = new ArrayList<Integer>();

        for (int value : values) {
            if (!contains(valuesToRemove, value)) {
                newValues.add(value);
            }
        }

        int[] newValuesArray = new int[newValues.size()];
        for (int i = 0; i < newValues.size(); i++) {
            newValuesArray[i] = newValues.get(i);
        }
        return newValuesArray;
    }

    public static <T> boolean contains(T[] values, T containedValue) {
        boolean isContained = false;
        int i = 0;
        while (!isContained && i < values.length) {
            isContained = containedValue.equals(values[i]);
            i++;
        }

        return isContained;
    }

    public static boolean contains(short[] values, short containedValue) {
        boolean isContained = false;
        int i = 0;
        while (!isContained && i < values.length) {
            isContained = containedValue == values[i];
            i++;
        }

        return isContained;
    }

    public static boolean contains(int[] values, int containedValue) {
        boolean isContained = false;
        int i = 0;
        while (!isContained && i < values.length) {
            isContained = containedValue == values[i];
            i++;
        }

        return isContained;
    }

    public static boolean equals(byte[] bytes, int start, byte[] comparisonBytes, int comparisonStart,
            int comparisonLength) {
        boolean isEqual = true;
        compLoop: for (int i = 0; i < comparisonLength; i++) {
            int index = start + i;
            int comparisonIndex = comparisonStart + i;
            isEqual = bytes[index] == comparisonBytes[comparisonIndex];
            if (!isEqual) {
                break compLoop;
            }
        }

        return isEqual;
    }
}

Related

  1. removePointerFromDesc(String[] desc)
  2. removeRepeats(T[] array)
  3. removeRows(int[][] array, Collection rowsIndices)
  4. removeTrailing(final byte[] id)
  5. removeUser(String[] array, String user)
  6. removeZeros(int n, byte[] message)