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

kimHS
  • 53
  • 1
  • 9

2 Answers2

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

Community
  • 1
  • 1
Gaurav Agarwal
  • 173
  • 1
  • 10
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;
}
Marco
  • 61
  • 7