1

I want to delete column that has NA value.

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.

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;

                }
            }

1 Answer 1

1

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; 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.