【问题标题】:Java: Determining the position of the value in CSV fileJava:确定值在 CSV 文件中的位置
【发布时间】:2014-09-17 11:59:29
【问题描述】:
我有一个 CSV 文件:包含 80 行和 80 列的 DOSE 计算值,我编写了代码来查找 CSV 文件中的最大值,如下所示
public double getMaxDose()
{
double dose=0.0;
for(DetEl det_el:det_els)
{
if (det_el.getDose()>dose)
dose=det_el.getDose();
}
return dose;
}
我想在一个80行列的文件中找到最大值的位置,我想知道最大值存在于哪一行哪一列
非常感谢任何提示
【问题讨论】:
标签:
java
csv
position
max
【解决方案1】:
您需要使用一个变量来获取最大值的位置,我们可以使用该位置值来获取行值和列值。
以下程序可能会对逻辑有所启发:
public class Dose {
public static void main(String... args) {
double[][] arr = new double[][] {
{0.0,1.0,3.0},
{2.0,55.0,8.0},
{98.0,9.0,67.0},
{7.0,-1.0,22.0}
};
double dose=0.0;
int maxPosition=0;
int counter = 0;
for(double[] tarr:arr)
{
for(double aval:tarr) {
counter++;
if (Double.compare(aval,dose) > 0) {
dose=aval;
maxPosition=counter;
}
}
}
System.out.println("Maximum value: "+dose);
System.out.println("Max value position: "+maxPosition);
System.out.println("Arr length: "+arr.length);
System.out.println("Sub Arr length: "+arr[0].length);
System.out.println("Row:"+((maxPosition/arr.length)+1) +
" Column:"+(maxPosition%arr[0].length==0?arr[0].length:maxPosition%arr[0].length));
}
}