如何在java中删除二维数组列 [英] how to delete 2D array column in java

查看:115
本文介绍了如何在java中删除二维数组列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除具有NA值的列。

I want to delete column that has NA value.

例如,当array [i,j]具有NA时,我想删除第i列。
find2DIndex查找NA值,removeNA是删除第i列。

example, when array[i,j] has NA, I want to remove the i-th column. find2DIndex find NA value, and removeNA is remove i-th column.

尽管努力删除NA值,但数组中仍然存在NA值。

despite effort to remove the NA value, there still exist NA value in array..

private static Point find2DIndex(Object[][] array, Object search) {

    if (search == null || array == null) return null;

    for (int rowIndex = 0; rowIndex < array.length; rowIndex++ ) {
       Object[] row = array[rowIndex];
       if (row != null) {
          for (int columnIndex = 0; columnIndex < row.length; columnIndex++) {
             if (search.equals(row[columnIndex])) {
                 return new Point(rowIndex, columnIndex);
             }
          }
       }
    }
    return null; 
 }


public static String[][] removeNA(String[][] str){

    Point index = new Point();

    if((index= find2DIndex(str,"NA"))!=null){
    for(int j=0;j<49;j++){
        for(int i=index.y;i<str.length/49;i++){
                str[j][i]= str[j][i+1];
            }//j열 모두 지우기
        }
    }
                return str;
}


public static void main(String[] args) throws IOException {
    String str = readCSV(new File("D:/sample.csv"));

    String[] strArr = parse(str); // String 배열에 차곡차곡 담겨서 나온다.

    String[][] Array2D = new String[27][45];

    for(int i=0; i<45;i++){
        for(int j=0;j<27;j++){
            String k = strArr[i*27+j];
             Array2D[j][i]= k;

                }
            }


推荐答案

除非更改销毁并使用新维度重建它,否则无法更改现有Array数据结构的维度(必须重新初始化并重新填充)。

You cannot change the dimension of an existing Array data-structure unless you destroy and rebuild it with the new dimension (you have to re-initialize and re-populate).

您可以尝试这样的事情。

You can try something like this.

public int[][] deleteColumn(int[][] args,int col)
{
    int[][] nargs;
    if(args != null && args.length > 0 && args[0].length > col)
    {
        nargs = new int[args.length][args[0].length-1];
        for(int i=0; i<args.length; i++)
        { 
            int newColIdx = 0;
            for(int j=0; j<args[i].length; j++)
            {
                if(j != col)
                {
                    nargs[i][newColIdx] = args[i][j];
                    newColIdx++;
                }               
            }
        }
    }
    return nargs; 
}

这篇关于如何在java中删除二维数组列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆