数组中的数据在哪里?

时间:2013-11-23 04:39:12

标签: java arrays sorting processing

我有一个Processing sketch,我正在尝试绘制一个我正在使用的数据集的散点图。我正在将.CSV文件加载到数组中没有问题,我可以将其全部格式化并将其放入“tokens”数组中以访问特定行中的信息。

问题似乎是在解析数据之后,并尝试映射信息以绘制图形,数组中的所有元素都返回0.0。

代码有点冗长,但我会尝试将其保留到相关位,并希望不会遗漏任何内容。

void setup(){
size(1024, 768)
smooth();

OfficinaBold36 = loadFont("OfficinaBold-36.vlw");

//Load data
googleData     = loadStrings("RobFordCorrelate.csv");

//Init all the arrays 
if((googleData == null){
println("Error, one of the datasets could not be loaded.");
}
else{

     totalSearch      = googleData.length-1;
     googleDates      = new int[totalSearch];
     relativeInterest = new float[totalSearch];

     //Also grabbing all column names for use later, if needed
     columnNames      = googleData[0].split(",");
 }

parseGoogleData();
}

这是parseGoogleData函数:

void parseGoogleData(){

/*Grab all the dates, we have to loop backwards through this set,
because the CSV was formatted in reverse chronological order.

Note that because of the row > 0 condition, we will not include
row 0, i.e the column title row */

for (int row = googleData.length-1 ; row > 0; row--){

 /*counter going up, we need a reference to a counter that
 counts up, while the main index counts down, to be
 able to assign certain values*/
 int i = 0;

 //Grab all the elements in that row, splitting them at the comma
 googleTokens = googleData[row].split(",");

 // Grab the elements we want to look at, the date, index 0
 googleDates[i]      = int(googleTokens[0]);

 //and relative interest in the search term  "rob ford", index 1
 relativeInterest[i] = float(googleTokens[1]);


 //increment our 2nd counter
 i++;
 }
}

relativeInterest数组也是一个全局变量,因此我可以在setup和draw中访问它。现在,如果我在最后一个for循环中println(relativeInterest[i]),它将返回所有正确的数据。但是,如果我在下面的draw()循环中打印它们,它们都返回零(我只包括引用绘制每个点的线,因为我有很多x和y轴的定位数据,我不想包括):

void draw(){
for(int row = 0 ; row < totalSearch; row++){
float y = map(relativeInterest[row], -3, 3, 0, width);
float x = row*lb;

int d = 5;

noStroke();
fill(#FFBA00, 180);
ellipse(x, y, d, d);
}

当我运行这段代码时,每个椭圆都在完全相同的y位置,我不知道数组中的数据何时设置为0?绘制循环中的东西不会访问数组,直到这些行被击中,但我不知道为什么它们被转换为0.0?

非常感谢任何/所有帮助。抱歉这么长的帖子!

1 个答案:

答案 0 :(得分:1)

将您的int i = 0;移到for循环之外。您还需要Integer.valueOfFloat.valueOf,而不是int()float()