[英]Retrieving second and third column from a 2d array to an array
I have a Java code that will output 3 column of double integer just like this (1 column, 2 column, 3 column): 我有一个Java代码,它将输出3列的双整数,如下所示(1列,2列,3列):
( 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! 谢谢!
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(") ");
}
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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.