1

when searched value exist in array, I choose the column and save them. for example

1 2 3 4 5 6
A B C D E F
G H I J K L

I want to make a column including x==1||x==4 below column will be result of what i want

1 4
A D
G J

below code is my 2D array code. I make 1D array from csv file and 2D array. when searched value exist, I choose the column and save them.

    String str = readCSV(new File("D:/sample_folder/sample1.csv"));
    String[] strArr = parse(str); // It comes out in a row in an String array.
    int varNumber = 45;
    int rowNumber = strArr.length/varNumber;

    String[][] Array2D = new String[varNumber][rowNumber];

    for(int j=0;j<varNumber;j++)
    {
        for(int i=0; i<rowNumber;i++)   
            {
                String k = strArr[i*varNumber+j];
                        Array2D[j][i]= k;
        }
    }                       //make 2D array         
2
  • Have you tried to approach the problem on your own? If so paste your code here :) If not try yourself and ask a more specific question when getting errors and research does not help. Commented Dec 1, 2016 at 9:59
  • What is the format of your csv ? It would be interesting to rotate your array. That way, you will be able to get the column by copying the Array2d[i] in one line. Please give an example to run this Commented Dec 1, 2016 at 10:52

2 Answers 2

1

You can through rows of 2D array and pick the column you want.

for(int j=0;j<rowNumber;j++)
{
    // index starts from 0
    yourArray[j][0] = array2D[j][0];
    yourArray[j][1] = array2D[j][3];
}

Or more dynamically you could write:

int[] columnsYouWant = {0, 3};
for(int j=0;j<rowNumber;j++)
{
    for(int c=0;c<columnsYouWant.length;c++)
    {
       yourArray[j][c] = array2D[j][columnsYouWant[c]];
    }
}

If you want to use if (x == 1 || x == 4) :

for(int j=0;j<rowNumber;j++)
{
    column = 0;
    for(int c=0;c<columnNumber;c++)
    {
       x = c + 1;
       if (x == 1 || x == 4)
       yourArray[j][column++] = array2D[j][c];
    }
}

I might get it wrong. It also seems you may want to have columns starting with 1 or 4. In that case, if your first row has numbers and rest are alphabetical. You should find the column starting with either 1 or 4.

for(int j=0;j<colNumber;j++)
{
   x = array2d[0][j];
   if ( x == 1 || x == 4 ) {
       // add you j to an array
   }
}

In the case you will know which columns you want, and you can use the second piece of code in my answer to create 2D array with columns you want.

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

1 Comment

I want to use if(x==1||x==4)
0

Try this simulation, I populate this in your 2DArray:

1 2 3 4 5 6
A B C D E F
G H I J K L

after that, I made a code to print only the columns 1 and 4.

public static void main(String[] args) {
    String[][] twoDArray = populateArray();
    int x = 0;
    for (int i = 0; i < twoDArray.length; i++) {
        for (int j = 0; j < twoDArray[0].length; j++) {
            x = j + 1;
            if(x == 1 || x == 4) {
                System.out.print(twoDArray[i][j]);
            }
        }
        System.out.println();
    }

}

public static String[][] populateArray() {
    String[][] twoDArray = new String[3][6];
    for (int i = 0; i < twoDArray[0].length; i++) {
        twoDArray[0][i] = (i + 1) + "";
    }
    char alphaChar = 'A';
    for (int i = 1; i < twoDArray.length; i++) {
        for (int j = 0; j < twoDArray[0].length; j++) {
            twoDArray[i][j] = String.valueOf(alphaChar);
            alphaChar++;
        }
    }
    return twoDArray;
}

the output of the code is:

14
AD
GJ

if you comment the if(x == 1 || x == 4) { that I used, it will print like this:

123456
ABCDEF
GHIJKL

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.