0

I stored 96 variables in a String 2D array. I want to get 49 variables from it. for example

variable1  variable2  variable3  variable4......  variable96    -> Original array : 97 variables
    5         6            3         5    ......      6
    6         8            8         1    ......      9
    10        4            4         9    ......      1
    .                                                 .
    .                                                 .
    .                                                 .
    2         ...          ...            ......      7

My purpose : If I select 45 variables (such as variable1, variable3, variable6, ....,variable86) then I want to make following 2D array.

variable1  variable3  variable6  ......  variable86 -> modified array: selected 45 variables
    5         3           15     ......      2
    6         8           21     ......      2
    10        4           9      ......      6
    .                                        .
    .                                        .
    .                                        .
    2       ...          ...     ......      7

I try to using the below code. but result is null null null null null....

public static String[][] haveVariable(String[][] strArr){
    String[][] twoDArray = new String[strArr.length][strArr[0].length];
    int copy = 0;
    for(int i=0; i<strArr.length;i++){
    if(strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="LSU_rO2Sig_mp[1]"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"
            ||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]==""||strArr[1][i]=="variable"||strArr[1][i]=="variable"
            ||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable"||strArr[1][i]=="variable")
    {       String tmp = null;
            for(int j=0; j<strArr[1].length;j++){
                 tmp= strArr[i][j];
                 twoDArray[copy][j]=tmp;
                System.out.println(tmp);
            }
            copy++;
    }
        }
    return twoDArray;
    }

I modify the code. but result is same...

    public static String[][] haveVariable(String[][] strArr){
        String[] varName= {"variable1", "variable2",    "variable3",    "variable4"};
    int selectedVariable= 45;// number of original data's variable
        String[][] twoDArray = new String[selectedVariable][strArr[0].length];
        int copy = 0;
        for(int i=0; i<selectedVariable;i++){

            if(strArr[i][0].equals(varName)){

                for(int j=0; j<strArr[0].length;j++){

                    String  tmp= strArr[i][j];
                    twoDArray[copy][j]=tmp;
                    copy++;
            }
        }
   }
        return twoDArray;
}

but result is same..i don't know what is the problem

3
  • 6
    You are using == instead of equals() function! stackoverflow.com/questions/7520432/… Commented Dec 2, 2016 at 10:58
  • Basicly, you want to remove some column of in array ? Commented Dec 2, 2016 at 10:58
  • Yes, I would like to delete the column I did not select. Commented Dec 2, 2016 at 12:35

2 Answers 2

2

You should not compare two string instances using == instead you should use .equals() method.

So, in your case the code

strArr[1][i]=="variable"

must be replaced with

strArr[1][i].equals("variable")

Check out this answer Comparing String in Java

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

1 Comment

I revise that. but result is not changed
0

Just for consistency I suggest you to ignore the labels (varable1, variable2...) inside an object. Your varuable name is your Label. Given a 2DArray you know that each columns refers to a variable. (You can use documentation to specify this).

public static int[][] haveVariable(String[][] strArr, int selectedVariable){
    int[][] twoDArray = new int[selectedVariable][strArr[0].length];
    int copy = 0;
    for(int i=0; i<selectedVariable;i++){
        for(int j=0; j<strArr[0].length;j++){
            tmp= strArr[i][j];
            twoDArray[i][j]=tmp;
            System.out.println(tmp);
        }
    }
    return twoDArray;
}

3 Comments

Where does the variable compare? I made a correction, but the result is not good.
[] . . . . . . 2 ... ... ...... 7
[ [1.....100], [1.....100], [1.....100], ] You should have somethig like this. You don't need String inside. You know colum1 is variable 1.

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.