0

I have a Java code that will output 3 column of double integer just like this (1 column, 2 column, 3 column):

( 0.09,  0.27,  0.01) 
( 0.00, -0.00,  0.26)  
( 0.02, -0.02,  0.24) 
( 0.22, -0.11, -0.03) 

Now, I wish to store all the values from the second column into an array and the values from the third column into another array. Is there a way I could modify it so that it will achieve that?

This is my partial code:

for (int i = 0; i < termVectors.length; ++i) {
    System.out.print("(");
    for (int k = 0; k < 3; ++k) {
    if (k > 0) System.out.print(", ");
    System.out.printf("% 5.2f",termVectors[i][k]);
    }
    System.out.print(")  ");
}

Thanks!

1
  • I suggest you to use Arrays.toString(double[]) to display your table in one line of code: for (double[] row:termVector) System.out.println(Arrays.toString(row)); Commented Feb 17, 2016 at 15:52

2 Answers 2

1

Please try the following code.

 int[] secondColVal = new int[termVectors.length];
int[] thirdColVal = new int[termVectors.length];

for (int i = 0; i < termVectors.length; ++i) {
    System.out.print("(");
    for (int k = 0; k < 3; ++k) {
    if (k > 0) System.out.print(", ");
    System.out.printf("% 5.2f",termVectors[i][k]);
    if(k==1)
    secondColVal[i] = termVectors[i][k];
    if(k==2)
    thirdColVal[i] = termVectors[i][k];
    }
    System.out.print(")  ");
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think this does the trick just that you are traversing all the elements of the termVectors with 2 for loops which is a bit complex in this case.
1

This should give you what you need :)

// since you're using the length multiple times, store it in a variable!
int len = termVectors.length;
// declare two arrays to represent your second and third columns
int[] secondColumn = new int[len];
int[] thirdColumn = new int[len];

for (int i=0;i<len;i++)
{
    // populate your arrays
    secondColumn[i] = termVectors[i][1];
    thirdColumn[i] = termVectors[i][2];
}

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.