我打印了双精度类型的文本文件中的数据,并将其转换为双精度数组。当我打印数组时,它看起来像这样
[2.0 -2.0 -2.0 -2.0 2.0 2.0 2.0 2.0] 1.39E-04
[-2.0 -2.0 0.0 -2.0 -2.0 0.0 -2.0 2.0] 0.020446
[2.0 0.0 -2.0 2.0 2.0 -2.0 -2.0 -2.0] 0.032339
[2.0 2.0 -2.0 -2.0 2.0 2.0 -2.0 2.0] 0.026673
[-2.0 0.0 -2.0 -2.0 0.0 2.0 0.0 2.0] 0.094135
[0.0 0.0 0.0 -2.0 0.0 2.0 2.0 0.0] 0.045922
[0.0 -2.0 0.0 -2.0 0.0 2.0 0.0 -2.0] 0.117043
[-2.0 -2.0 -2.0 2.0 2.0 2.0 2.0 -2.0] 0.425709
[-2.0 -2.0 -2.0 2.0 2.0 2.0 2.0 2.0] 0.156286最后一列是根据特定方程计算的每一行的分数。我的问题是如何在循环之外使用这个数组?因为我需要对这个数组做一些处理。抱歉,我需要在循环外部使用具有相同结构但没有括号的数组
我使用的代码是这样的
double [] D =new double [rows];
for (int i = 0; i < cols-1 i++) {
List<Double> list= new ArrayList<Double>();
for (int j = 0; j < rows; j++) {
int A = M[c][r];
D[c] = A;
list.add(D[c]);
}
double score= M.calculateDistance(D,class);
System.out.println(list+" "+score);
}发布于 2019-07-17 09:34:23
您可以创建另一个保存数组的列表
List<List<Double>> listOfList= new ArrayList<List<Double>>();因此,您的新代码将如下所示:
double [] D =new double [rows];
List<List<Double>> listOfList= new ArrayList<List<Double>>();
for (int i = 0; i < cols-1 i++) {
List<Double> list= new ArrayList<Double>();
for (int j = 0; j < rows; j++) {
int A = M[c][r];
D[c] = A;
list.add(D[c]);
}
double score= M.calculateDistance(D,class);
System.out.println(list+" "+score);
listOfList.add(list); // add the array
}但是,如果您还想在循环之外使用score并将其与特定的数组相关联,我建议您使用Map或创建自己的对象。
https://stackoverflow.com/questions/57067213
复制相似问题