英文:
Looping over multiple objects in Java
问题
Context
我正在尝试从这个代码库中循环遍历多个数据系列,以下是代码示例:
// 默认配置所有内容
Plot plot = Plot.plot(null).
// 为数据系列一设置数据
series("数据系列一", Plot.data().
xy(1, 2).
xy(3, 4), null);
// 为数据系列二设置数据
series("数据系列二", Plot.data().
xy(10, 20).
xy(30, 40), null);
// 保存为 sample_minimal.png
plot.save("sample_minimal", "png");
然而,数据系列的数量(两个 List<Double> x
和 List<Double> y
对象的ArrayList)目前是硬编码的,我尝试循环遍历任意数量的数据系列/线条时遇到了一些困难。
特别是,我认为我可以像这样写一个循环:
for (int i = 0; i < y_data_series.size(); i++) {
series(f'Data Series {i}', Plot.data().
xy(x_data_series.get(i), y_data_series.get(i)),
但是 Plot plot = Plot.plot(null).
中的句点暗示着需要一个系列方法,而不是一个for循环。
Question
我如何在 Plot 对象内部循环遍历这两个数据系列的 ArrayList?
Doubts
- 我认为我的问题有点不明确,因为我还不太清楚在两个系列方法之间使用句点时语法是如何工作的。但我希望上下文能够澄清问题所在。
- 我认为
zip
/map
/fold
可能是一种选择,因为它们可能和 series 方法一样是函数。
英文:
Context
I am trying to loop over multiple dataseries in the following code from this repo:
// configuring everything by default
Plot plot = Plot.plot(null).
// setting data for data series one
series("dataseries One", Plot.data().
xy(1, 2).
xy(3, 4), null);
// setting data for data series two
series("dataseries Two", Plot.data().
xy(10, 20).
xy(30, 40), null);
// saving sample_minimal.png
plot.save("sample_minimal", "png");
However, the number of dataseries (two arraylist of List<Double> x
and List<Double> y
objects), is currently hardcoded and I am experiencing some difficulties looping through an arbitrary amount of dataseries/lines.
In particular I thought I could write a for loop like:
for(int i = 0; i < y_data_series; i++){
series(f'Data Series {i}', Plot.data().
xy(x_data_series.get(i),y_data_series.get(i)),
But the dot at the end of Plot plot = Plot.plot(null).
implies a series method is expected instead of a for loop.
Question
How could I loop over the two arraylists of dataseries within the Plot object?
Doubts
- I think my question is a bit ill-posed because I do not yet exactly know how the syntax works, when a series method is "called" on another series method with a dot in between. I hope the context however clarifies what the question is.
- I would think a
zip
/map
/fold
might be an option as they might be functions just like the series method.
答案1
得分: 1
我对你的问题不太确定,但我认为以下的代码应该适合你的需要:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PlotTest {
public static void main(String[] args) {
Plot plot = Plot.plot(null);
final List<List<Double>> multipleXseries = new ArrayList<List<Double>>();
final List<List<Double>> multipleYseries = new ArrayList<List<Double>>();
final List<Double> xSeries1 = new ArrayList<Double>();
final List<Double> ySeries1 = new ArrayList<Double>();
xSeries1.add(0D); ySeries1.add(10D);
xSeries1.add(1D); ySeries1.add(20D);
xSeries1.add(2D); ySeries1.add(30D);
multipleXseries.add(xSeries1);
multipleYseries.add(ySeries1);
final List<Double> xSeries2 = new ArrayList<Double>();
final List<Double> ySeries2 = new ArrayList<Double>();
xSeries2.add(0D); ySeries2.add(15D);
xSeries2.add(1D); ySeries2.add(25D);
xSeries2.add(2D); ySeries2.add(35D);
multipleXseries.add(xSeries2);
multipleYseries.add(ySeries2);
for(int i = 0; i < multipleXseries.size(); i++) {
Plot.Data data = Plot.data().xy(multipleXseries.get(i), multipleYseries.get(i));
plot = plot.series("数据系列 " + i, data, null);
}
try {
plot.save("test", "png");
} catch (IOException e) {
e.printStackTrace();
}
}
}
英文:
I am not exactly sure on whether I understand your question, but I think something along these lines should work for you:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PlotTest {
public static void main(String[] args) {
Plot plot = Plot.plot(null);
final List<List<Double>> multipleXseries = new ArrayList<List<Double>>();
final List<List<Double>> multipleYseries = new ArrayList<List<Double>>();
final List<Double> xSeries1 = new ArrayList<Double>();
final List<Double> ySeries1 = new ArrayList<Double>();
xSeries1.add(0D);ySeries1.add(10D);
xSeries1.add(1D);ySeries1.add(20D);
xSeries1.add(2D);ySeries1.add(30D);
multipleXseries.add(xSeries1);
multipleYseries.add(ySeries1);
final List<Double> xSeries2 = new ArrayList<Double>();
final List<Double> ySeries2 = new ArrayList<Double>();
xSeries2.add(0D);ySeries2.add(15D);
xSeries2.add(1D);ySeries2.add(25D);
xSeries2.add(2D);ySeries2.add(35D);
multipleXseries.add(xSeries2);
multipleYseries.add(ySeries2);
for(int i = 0; i < multipleXseries.size(); i++) {
Plot.Data data = Plot.data().xy(multipleXseries.get(i), multipleYseries.get(i));
plot = plot.series("Data Series " + i, data, null);
}
try {
plot.save("test", "png");
} catch (IOException e) {
e.printStackTrace();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论