Java Array Remove removeFromArray(String remove, String array[])

Here you can find the source of removeFromArray(String remove, String array[])

Description

Remove every element that matches a string from an array.

License

Open Source License

Parameter

Parameter Description
remove The element to be removed.
array The String array to remove the element from.

Return

A new array.

Declaration

public static String[] removeFromArray(String remove, String array[]) 

Method Source Code

//package com.java2s;
/*/*  www . jav a 2s.co m*/
 * Copyright 2015 The OpenDCT Authors. All Rights Reserved
 *
 * 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;

public class Main {
    /**
     * Remove every element that matches a string from an array.
     *
     * @param remove The element to be removed.
     * @param array The String array to remove the element from.
     * @return A new array.
     */
    public static String[] removeFromArray(String remove, String array[]) {
        ArrayList<String> returnValues = new ArrayList<>();

        for (String value : array) {
            if (!value.equals(remove)) {
                returnValues.add(value);
            }
        }

        return returnValues.toArray(new String[returnValues.size()]);
    }
}

Related

  1. removeFirstElementFromArray(T[] array)
  2. removeFirstTwoArgs(String[] args, int startIndex)
  3. removeFlag(String[] options, String flag)
  4. removeFlagsFromArgs(String[] args)
  5. removeFrom(double[] source, int idx)
  6. removeFromArray(String[] oldArray, String stringToRemove)
  7. removeId(byte[] in, Integer id)
  8. removeIndex(String[] args, int index)
  9. removeIndex(T[] array, int index)