问题
i have this piece of code. To explain:
The user inputs "initcores" data and "ttime" data (the "fcores" is a result).
I want to fill the x array with values from 0 to ttime and the y from initcores to fcores and do the scatter plot ,x vs y.
I have one problem :
If i put " for (double t=0;t<=fcores;t=t+fcores/10.0){ y.add(t); "
it gives me a plot but its wrong.
if i put " for (double t=initcores ;t<=fcores;..." (which is right because we are starting from initcores)
it doesn't appear anything in the plot.
Am i not doing sth right here?
Thank you!
.........
Double initcores= getInitcores();
Double fcores= getFcores();
Double ttime=getTime();
ArrayList<Double> x =new ArrayList<Double>();
ArrayList<Double> y =new ArrayList<Double>();
//fill x,y values
for (double t=0;t<=ttime;t+=ttime/10.0){
x.add(t);
}
for (double t=initcores;t<=fcores;t+=fcores/10.0){
y.add(t);
}
TimeSeries series = new TimeSeries("Number of cores");
for (int i=0;i<x.size();i++){
for (int j=0;j<y.size();j++){
series.add(i,j);
}
}
..........
--------------EDIT --------------------------------------
If i use :
double [] x = {0.0,ttime}; //time axis
double [] y = {initcores,fcores}; //number of cores axis
TimeSeries series = new TimeSeries("Number of cores");
for (int i=0;i<x.length;i++){
series.add(x[i],y[i]);
}
it gives me a plot with only 2 points.That's why i am trying to fill the points between them (for x axis :0-ttime ,for y axis:initcores-fcores).
回答1:
You got your for
-loops wrong. Use the following ones and you should be fine:
for (int i=0;i<=10;i++){
x.add(ttime / 10.0 * i);
}
for (int i=0;i<=10;i++){
y.add(initcores + ((fcores - initcores) / 10 * i));
}
These loops will give you 11 points, but you can adjust them to your needs.
Update
You naturally have to use the correct values from the list. Make sure that both lists have same length.
ArrayList<Double> x = new ArrayList<Double>();
ArrayList<Double> y = new ArrayList<Double>();
... above code ...
TimeSeries series = new TimeSeries("Number of cores");
for (int i=0;i<x.size();i++){
series.add(x.get(i),y.get(i));
}
来源:https://stackoverflow.com/questions/8954682/how-to-fill-array-list-between-2-points