Java 在二维阵列中删除行的最快方法

Java 在二维阵列中删除行的最快方法,java,arrays,optimization,arraylist,clone,Java,Arrays,Optimization,Arraylist,Clone,我有一个程序,需要从byte[][]数组中删除一组行。我想删除所有没有零的行。现在我正在使用以下方法: public byte[][] checkRows(byte[][] grid) { LinkedList<Integer> temp = new LinkedList<Integer>(); boolean willRemove = false; for (int y = 0; y < grid.length; y++) {

我有一个程序,需要从
byte[][]
数组中删除一组行。我想删除所有没有零的行。现在我正在使用以下方法:

public byte[][] checkRows(byte[][] grid) {
    LinkedList<Integer> temp = new LinkedList<Integer>();
    boolean willRemove = false;
    for (int y = 0; y < grid.length; y++) {
        boolean tempCheck = true;
        for (int x = 0; x < grid[0].length; x++) {
            if (grid[y][x] == 0) {
                tempCheck = false;
                break;
            }
        }
        if (tempCheck) {
            temp.add(y);
            willRemove = true;
        }
    }

    if (willRemove) {
        int[] rowArray = convert(temp);
        return removeRows(grid, rowArray);
    }
    return grid;

}

public byte[][] removeRows(byte[][] grid, int[] rows) {
    int total = rows.length;
    int current = 0;
    byte[][] retGrid = new byte[grid.length][grid[0].length];
    for (int i = total; i < grid.length; i++) {
        if (current < total && i-total+current == rows[current]) {
            current++;
        }
        //retGrid[i] = grid[i-total+current].clone();
        System.arraycopy(grid[i-total+current], 0, retGrid[i], 0, xsize);

    }
    return retGrid;
}

public int[] convert(LinkedList<Integer> intList) {
    int[] retArray = new int[intList.size()];
    for (int i = 0; i < retArray.length; i++) {
        retArray[i] = intList.get(i).intValue();
    }
    return retArray;
}
公共字节[][]检查行(字节[][]网格){
LinkedList temp=新建LinkedList();
布尔值willRemove=false;
对于(int y=0;y
这为我提供了一种相当快速的方法,可以从2D数组中删除行,并在数组顶部用零行替换它们。有没有更快的方法达到同样的效果

如果不清楚脚本的作用,那就是在俄罗斯方块游戏中删除整行


更新:使用
System.arraycopy()
而不是
clone()
可为小型阵列提供5%的性能提升。

使用链表将导致0(1)次删除,请参阅,因为无论如何都必须迭代该列表

起初我认为多维数组是紧凑的,因为它是一个连续的内存块,但事实并非如此。因此,您不会失去任何可能已经生效的缓存好处

遗憾的是Java没有值类型(目前),我会使用一个而不是一个字节来编码信息。这不是绝对必要的


从代码审查的角度来看,在方法
checkRows
中使用bool
将删除
,这是不必要的,因为在这些情况下,
temp
将有多个元素。如果不需要的话,我会尝试完全消除
ArrayList
分配-推迟它。

使用链表会导致O(1)个删除,请参阅,因为无论如何都必须对列表进行迭代

起初我认为多维数组是紧凑的,因为它是一个连续的内存块,但事实并非如此。因此,您不会失去任何可能已经生效的缓存好处

遗憾的是Java没有值类型(目前),我会使用一个而不是一个字节来编码信息。这不是绝对必要的


从代码审查的角度来看,在方法
checkRows
中使用bool
将删除
,这是不必要的,因为在这些情况下,
temp
将有多个元素。如果不需要,我会尝试完全消除
ArrayList
分配-推迟它。

这个小方法执行一维情况下问题所需的功能

private static final void onClearFilledRows(final byte[] pTetris, final int pRowLength) {
        int i = 0, j = 0;
        /* Track the last valid position of row data. */
        int lLastValidIndex = 0;
        /* Iterate through each row. */
        for(i = 0; i < pTetris.length; i += pRowLength) {
            /* Iterate through each cell in the row. */
            boolean lContainsZero = false;
            for(j = i; j < (i + pRowLength) & !lContainsZero; j++) {
                lContainsZero |= pTetris[j] == 0;
            }
            /* This row is valid. Copy it to the last valid index. */
            if(lContainsZero) {
                System.arraycopy(pTetris, i, pTetris, (lLastValidIndex++ * pRowLength), pRowLength);
            }
        }
        /* Empty the remaining rows. */
        for(i = lLastValidIndex * pRowLength; i < pTetris.length; i++) {
            /* Set the element to zero. */
            pTetris[i] = 0;
        }
    }
private static final void onClearFilledRows(final byte[]pTetris,final int pRowLength){
int i=0,j=0;
/*跟踪行数据的最后一个有效位置*/
int lLastValidIndex=0;
/*遍历每一行*/
对于(i=0;i
然后,可以针对二维情况重新处理该逻辑:

private static final void onClearFilledRows(final byte[][] pTetris) {
        int i = 0, j = 0;
        /* Track the last valid position of row data. */
        int lLastValidIndex = 0;
        /* Iterate through each row. */
        for(i = 0; i < pTetris.length; i ++) {
            /* Iterate through each cell in the row. */
            boolean lContainsZero = false;
            for(j = 0; j < pTetris[i].length & !lContainsZero; j++) {
                lContainsZero |= pTetris[i][j] == 0;
            }
            /* This row is valid. Copy it to the last valid index. */
            if(lContainsZero) {
                System.arraycopy(pTetris[i], 0, pTetris[lLastValidIndex++], 0, pTetris[i].length);
            }
        }
        /* Empty the remaining rows. */
        for(i = lLastValidIndex; i < pTetris.length; i++) {
            for(j = 0; j < pTetris[i].length; j++) {
                /* Set the element to zero. */
                pTetris[i][j] = 0;
            }
        }
    }
私有静态最终void onClearFilledRows(最终字节[][]pTetris){
int i=0,j=0;
/*跟踪行数据的最后一个有效位置*/
int lLastValidIndex=0;
/*遍历每一行*/
对于(i=0;i
这个小方法执行一维情况下问题所需的功能

private static final void onClearFilledRows(final byte[] pTetris, final int pRowLength) {
        int i = 0, j = 0;
        /* Track the last valid position of row data. */
        int lLastValidIndex = 0;
        /* Iterate through each row. */
        for(i = 0; i < pTetris.length; i += pRowLength) {
            /* Iterate through each cell in the row. */
            boolean lContainsZero = false;
            for(j = i; j < (i + pRowLength) & !lContainsZero; j++) {
                lContainsZero |= pTetris[j] == 0;
            }
            /* This row is valid. Copy it to the last valid index. */
            if(lContainsZero) {
                System.arraycopy(pTetris, i, pTetris, (lLastValidIndex++ * pRowLength), pRowLength);
            }
        }
        /* Empty the remaining rows. */
        for(i = lLastValidIndex * pRowLength; i < pTetris.length; i++) {
            /* Set the element to zero. */
            pTetris[i] = 0;
        }
    }
private static final void onClearFilledRows(final byte[]pTetris,final int pRowLength){
int i=0,j=0;
/*跟踪行数据的最后一个有效位置*/
int lLastValidIndex=0;
/*遍历每一行*/
对于(i=0;i