0

I want to make 2D array that Na removed. If NA is in fifth column and second row, then I want to remove second row. my code's step] 1step: data loading (1D array) 2step: make 2D array 3step: find specific value's column and row 4step: its row remove from array

but result is so bad.. help me

public static String[][] removeNA(String[][] str,int varNumber){
    String deleteMe="NA";
    String[][] na2Arr= new String[varNumber][str.length/varNumber]; 
            for(int i=0;i<varNumber;i++)
            {
                for(int j=0;j<str.length/varNumber;j++){
                String  tmp=str[i][j];
                if(tmp.equals(deleteMe))//참이면 [i,j] 중 j가 포함된거 모두 제거
                    {
                    for(int k=0;k<varNumber;k++){
                        String tmp1=str[k][j+1];
                        na2Arr[k][j]=tmp1;
                            }
                        }
                else{
                    for(int k=0;k<varNumber;k++){
                        String tmp1=str[k][j];
                        na2Arr[k][j]=tmp1;
                        }
                    }
                }
            }
                return na2Arr;
        }


public static void main(String[] args) {
    String str = readCSV(new File("D:/sample2.csv"));
    String[] strArr = parse(str); // String 배열에 차곡차곡 담겨서 나온다.
    int varNumber = 45;
    int rowNumber = strArr.length/varNumber;
    String[][] Array2D = new String[varNumber][rowNumber];


    for(int i=0; i<rowNumber;i++)
    {
        for(int j=0;j<varNumber;j++)
            {
                String k = strArr[i*varNumber+j];
                        Array2D[j][i]= k;
            }
    }
    String[][] naArray2D=removeNA(Array2D,varNumber);

    /*      for(int i=0; i<naArray1D.length;i++){
        System.out.println(naArray1D[i]);
        }*/
                for(int i=0;i<varNumber;i++){
                        for(int j=0;j<naArray2D.length/varNumber;j++){
                            System.out.println(naArray2D[i][j]);
                        }
                        System.out.println("**********************N A제거&2차원 배열**********************");
                }


}
4
  • So if you have a String[45][10] and one cell contains NA, then you want to remove that column so you get a String[45][9], is that it? Commented Nov 28, 2016 at 5:22
  • Is it possible that there are more than one such column that should be removed? Could you end up having no columns at all? Commented Nov 28, 2016 at 5:25
  • Thx for your reply and Your question about having more than 1 column removed and no column at all is possible if there exist more than 1 NA need to be removed Commented Nov 28, 2016 at 8:04
  • Related: how to delete 2D array column in java. Commented Nov 28, 2016 at 12:28

1 Answer 1

1

This should solve it:

public static String[][] removeNA(String[][] str, int varNumber) {
    String[][] na2Arr = new String[str.length][varNumber];
    // iterate over columns in str
    int columnsCopied = 0;
    for (int col = 0; col < varNumber; col++) {
        if (! columnContainsNa(col, str)) {
            // copy column to result
            for (int row = 0; row < str.length; row++) {
                na2Arr[row][columnsCopied] = str[row][col];
            }
            columnsCopied++;
        }
    }
    if (columnsCopied < varNumber) {
        // shorten each inner array to the actual number of columns (leave out if you don’t want this)
        for (int row = 0; row < str.length; row++) {
            na2Arr[row] = Arrays.copyOf(na2Arr[row], columnsCopied);
        }
    }
    return na2Arr;
}

private static boolean columnContainsNa(int col, String[][] str) {
    String deleteMe = "NA";
    for (int row = 0; row < str.length; row++) {
        if (str[row][col].equals(deleteMe)) {
            return true;
        }
    }
    return false;
}

I changed variable names i and j to row and col, I think it’s easier to read this way. The columnsCopied variable makes sure each column is copied into the right column of the result array even when more columns contain NA and are deleted. Also I think it’s simpler with the auxiliary method, columnContainsNa.

Please remember to mark the answer as accepted (on the tick mark to the left) if you think it is correct and helpful.

Sign up to request clarification or add additional context in comments.

9 Comments

thank you for your answer and kindness..there are a small problem...your answer reverse the column and rows, I am trying to fix the problem.. but it doesn't go well..
In that case I misunderstood. So if you have a String[45][10] and one cell contains NA, then you want to remove that column so you get a String[44][10], is that it?
uh.. I set array to array[varNumber][data.length/varNumber]
Is varNumber the number of rows or the number of columns? If you will excuse me, you could use a better variable name.
Thank you. I solve the problem!! length is number of row in 2D array. so I revise col and row. so code do well work. thank you!!
|

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.