I am trying to write 4 fields of type double and 1 field of type string to an excel file.
我试图写一个类型为double的4个字段和1个字符串类型的字段到excel文件。
Vector<test_class.sigma_results> R = MainClass.calculateAllNames(R1, R2, Names);
Vector
This is the method that calculates what I need for a specific stock name:
这是计算特定股票名称所需的方法:
public sigma_results calculateSigmas(Vector<Double> r1, Vector<Double> r2, String name) {
double[] W = {3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 22, 25};
sigma_results r = new sigma_results();
r.sigma = sumSquared(r1,20);
r.sigma_weighted_s2 = sumSquaredWeightedS2(r1, W, 20);
r.sigma_weighted_s3 = sumSquaredWeightedS3(r1, W, 20);
r.sigma_adj_weighted_s2 = sumSquaredWeightedS2(r2, W, 20);
r.sigma_adj_weighted_s3 = sumSquaredWeightedS3(r2, W, 20);
r.sigma_adj = sumSquared(r2,20);
r.name = name;
return r;
}
And this is where I calculate the values for all stocks in my universe:
这是我计算宇宙中所有股票的价值的地方:
public Vector calculateAllNames(Vector R1, Vector R2, Vector Names) {
public Vector calculateAllNames(Vector R1,Vector R2,Vector Names){
Vector<sigma_results> R = new Vector<sigma_results>(1000);
String current_name = "";
Vector<Double> r1 = new Vector<Double>(20);
Vector<Double> r2 = new Vector<Double>(20);
for (int idx=0; idx<Names.size(); idx++) {
if (Names.elementAt(idx).equals(current_name)) {
r1.add(R1.elementAt(idx));
r2.add(R2.elementAt(idx));
} else {
//calculation here
if (r1.size()>0) {
R.add(calculateSigmas(r1, r2, current_name));
}
r1.clear();
r2.clear();
current_name = Names.elementAt(idx);
r1.add(R1.elementAt(idx));
r2.add(R2.elementAt(idx));
}
}
return R;
}
So, I need to write each of these fields into a column in excel so that I can have my output corresponding to each individual stock name. What is the best way to write these individual fields to excel? I am unsure exactly how to access them in the first place, which is part of the problem. Still very new to Object Oriented Programming as I was working MATLAB for a while. Closest thing we had was a structure....
因此,我需要将每个字段写入excel中的一列,以便我可以将输出对应于每个单独的库存名称。将这些单独的字段编写为excel的最佳方法是什么?我不确定如何首先访问它们,这是问题的一部分。面向对象编程仍然很新,因为我在MATLAB工作了一段时间。我们最接近的是一个结构....
1 个解决方案
#1
0
Vector class makes me think this is Java. I'm not quite sure what field you wish to write to Excel (I'm guessing the name of the stock and some of the r.sigma... values.
Vector类让我觉得这是Java。我不太确定你想写什么字段到Excel(我猜的是股票的名称和一些r.sigma ...值。
What I would probably do is write a Comma separated values file. Something like this:
我可能会做的是写一个逗号分隔值文件。像这样的东西:
PrintWriter writer = new PrintWriter("c:\\export.csv", "UTF-8");
for(sigma_results s : R){
string oneLine = s.name + "," + s.firstField + "," s.secondField + "," + s.thirdField + "," + s.fourthField;
// write the line to the file
writer.println(oneLine);
}
writer.close();
#1
0
Vector class makes me think this is Java. I'm not quite sure what field you wish to write to Excel (I'm guessing the name of the stock and some of the r.sigma... values.
Vector类让我觉得这是Java。我不太确定你想写什么字段到Excel(我猜的是股票的名称和一些r.sigma ...值。
What I would probably do is write a Comma separated values file. Something like this:
我可能会做的是写一个逗号分隔值文件。像这样的东西:
PrintWriter writer = new PrintWriter("c:\\export.csv", "UTF-8");
for(sigma_results s : R){
string oneLine = s.name + "," + s.firstField + "," s.secondField + "," + s.thirdField + "," + s.fourthField;
// write the line to the file
writer.println(oneLine);
}
writer.close();