簡體   English   中英

從二維數組中刪除 java 中的列

[英]Removing a column in java from a 2d array

我正在嘗試編寫一個 java 方法,該方法將采用一個二維數組並將內容添加到一個新的二維數組中,期望指定行。 所以如果我有二維數組

1234
1234
1234

我想刪除第 3 列,我想得到

124
124
124

問題是我不知道如何讓它工作。 我能想到的最好方法是以下方法。

private static int[][] removeCol(int [][] array, int colRemove)
{
    int row = array.length;
    int col = array[0].length;

    int [][] newArray = new int[row][col];

    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            if(j != colRemove)
            {
                newArray[i][j] = array[i][j];
            }
        }
    }

    return newEx;
}

現在這個方法將返回這個

1204
1204
1204

但如果我能得到我想要的結果,效果會更好。 有沒有辦法做到這一點,或者我堅持我目前的結果?

您可以使用一個變量currColumn來指示當前列的位置,並且結果數組將比原始列少一列。 所以根據這個你可以改變你的代碼。

private static int[][] removeCol(int [][] array, int colRemove)
{
    int row = array.length;
    int col = array[0].length;

    int [][] newArray = new int[row][col-1]; //new Array will have one column less


    for(int i = 0; i < row; i++)
    {
        for(int j = 0,currColumn=0; j < col; j++)
        {
            if(j != colRemove)
            {
                newArray[i][currColumn++] = array[i][j];
            }
        }
    }

    return newEx;
}

另一種更好的方法是使用像ArrayList這樣的動態結構。 所以在這里你需要有Array of ArrayList然后你可以用remove()方法刪除元素。 如果您想更新任何元素,則可以使用set()方法。

只需使用另一個不會在循環運行時自動遞增的索引:

private static int[][] removeCol(int [][] array, int colRemove)
 {
int row = array.length;
int col = array[0].length-1;
int oldCol = array[0].length;

int [][] newArray = new int[row][col];

for(int i = 0; i < row; i++)
{
    for(int j = 0, k=0; j < oldCol && k < col; j++)
    {
        if(j != colRemove)
        {
            newArray[i][k++] = array[i][j];
        }
    }
}

return newArray;
}

通過將邏輯保持為基於0的列號:

    private static int[][] removeCol(int[][] array, int colRemove) {
    int row = array.length;
    int col = array[0].length;

    int[][] newArray = new int[row][col - 1]; // You will have one column less

    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        if (j != colRemove) {
          newArray[i][j > colRemove ? j -1 : j] = array[i][j]; // If you're looking at an index greater than the one to remove, you have to reduce index by one
        }
      }
    }

    return newArray;
  }

在第二次迭代中,檢查colRemove值並移動+1以進行其他迭代。 請參閱下面的代碼

private static int[][] removeCol(int [][] array, int colRemove)
{
    int row = array.length;
    int col = array[0].length-1;

    int [][] newArray = new int[row][col];

    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            if(j>=colRemove){
                 newArray[i][j] = array[i][j+1];
            }
            else{
                newArray[i][j] = array[i][j];
            }
        }
    }

    return newArray;
}

實際上,您使用默認值int替換要刪除的元素,即: 0

如果它不是要刪除的元素,則復制元素:

if(j != colRemove)
{
     newArray[i][j] = array[i][j];
}

否則你什么也不做(因此它從int獲取默認的0值)。

您應該從1減少新創建的數組的第二維。

你可以這樣跟着:

  • 使用全局循環迭代行(數組的第一個維度)

  • 使用兩個連續的內部循環來迭代列(數組的第二個維度)。
    第一個迭代直到“要刪除的列 - 1”並在新數組中生成元素的簡單副本。
    第二個從“要刪除的列”開始,並向左移動到新數組中原始數組的每個元素。

這是一個有效的代碼:

import java.util.Arrays;

public class Array2D {

    public static void main(String[] args) {

        int[][] arrayOriginal = new int[][]{{1,2,3,4},{1,2,3,4},{1,2,3,4}};
        int[][] arrayNew = removeCol(arrayOriginal, 2);
        System.out.println(Arrays.deepToString(arrayNew));;
    }
    private static int[][] removeCol(int [][] array, int colRemove)
    {
        int row = array.length;
        int col = array[0].length;

        int [][] newArray = new int[row][col-1];

        for(int i = 0; i < row; i++)
        {
            for(int j = 0; j < colRemove; j++)
            {              
                newArray[i][j] = array[i][j];                
            }

            for(int j = colRemove; j < col-1; j++)
            {                          
               newArray[i][j] = array[i][j+1];
            }

        }

        return newArray;
    }
}

輸出是:

[[1, 2, 4], [1, 2, 4], [1, 2, 4]]

也許這不是盡可能有效,但我確實工作:

public String[][] colDel(String[][] array, int deletedCol) {
    if (deletedCol > array[0].length - 1) {
        return null;
    }
    var result = new String[array.length][];
    int n = 0;
    ArrayList<String> tmp;
    for (var row : array) {
        tmp = new ArrayList<>(Arrays.asList(row));
        tmp.remove(deletedCol);
        result[n++] = tmp.toArray(String[]::new);
    }
    return result;
}

簡而言之,為每一行創建一個 ArrayList 的實例。 然后刪除第 n 列,如第二個參數所示。 最后將 arraylist 轉換回 String[] 的實例,並返回結果 String[][]。

干杯,

科蒂

public static String[][] dropColumnFromTable(String[][] table, int colNumber) {
    int rows = table.length;
    int col = table[0].length - 1;
    String[][] result = new String[rows][col];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < col+1; j++) {
            if (j < colNumber) {
                result[i][j] = table[i][j];
            } else if (j == colNumber) {
                // Do nothing
            } else if (j > colNumber) {
                result[i][j-1] = table[i][j ];
            }
        }
    }
    return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM