Java Array Remove removeRows(int[][] array, Collection rowsIndices)

Here you can find the source of removeRows(int[][] array, Collection rowsIndices)

Description

remove Rows

License

Open Source License

Parameter

Parameter Description
array The array
rowsIndices The rows to be removed

Return

A copy of the given array with the specified rows removed

Declaration

public static int[][] removeRows(int[][] array, Collection<Integer> rowsIndices) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class Main {
    /**//from w ww .ja  v  a 2 s  .c o  m
     * @brief Returns a copy of a given array with the specified rows removed
     *
     * @param array
     *     The array
     * @param rowsIndices
     *     The rows to be removed
     *
     * @return A copy of the given array with the specified rows removed
     */
    public static int[][] removeRows(int[][] array, Collection<Integer> rowsIndices) {
        int newLength = array.length - rowsIndices.size();
        int[][] newArray = new int[newLength][];

        ArrayList<Integer> listOfRows = new ArrayList<Integer>(rowsIndices);
        Collections.sort(listOfRows);

        int currentRowInNewArray = 0;
        int currentIndexInArray = 0;
        for (int i = 0; i < array.length; i++) {
            if (currentIndexInArray < listOfRows.size() && i == listOfRows.get(currentIndexInArray)) {
                currentIndexInArray++;
                continue;
            }

            newArray[currentRowInNewArray] = array[i];
            currentRowInNewArray++;
        }

        return newArray;
    }
}

Related

  1. removeNulls(T[] input)
  2. removeNullsFromStringArray(String[] array)
  3. removeOwnCookieNameFromCookieHeader(String ownCookieName, String[] cookies)
  4. removePointerFromDesc(String[] desc)
  5. removeRepeats(T[] array)
  6. removeTrailing(final byte[] id)
  7. removeUser(String[] array, String user)
  8. removeValues(int[] values, int[] valuesToRemove)
  9. removeZeros(int n, byte[] message)