在Java中循环遍历多个对象:

huangapple go评论93阅读模式
英文:

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> xList<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

  1. 我认为我的问题有点不明确,因为我还不太清楚在两个系列方法之间使用句点时语法是如何工作的。但我希望上下文能够澄清问题所在。
  2. 我认为 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(&quot;dataseries One&quot;, Plot.data().
        xy(1, 2).
        xy(3, 4), null);
    // setting data for data series two
    series(&quot;dataseries Two&quot;, Plot.data().
        xy(10, 20).
        xy(30, 40), null);

// saving sample_minimal.png
plot.save(&quot;sample_minimal&quot;, &quot;png&quot;);

However, the number of dataseries (two arraylist of List&lt;Double&gt; x and List&lt;Double&gt; 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 &lt; y_data_series; i++){
    series(f&#39;Data Series {i}&#39;, 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

  1. 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.
  2. 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&lt;List&lt;Double&gt;&gt; multipleXseries = new ArrayList&lt;List&lt;Double&gt;&gt;();
final List&lt;List&lt;Double&gt;&gt; multipleYseries = new ArrayList&lt;List&lt;Double&gt;&gt;();
final List&lt;Double&gt; xSeries1 = new ArrayList&lt;Double&gt;();
final List&lt;Double&gt; ySeries1 = new ArrayList&lt;Double&gt;();
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&lt;Double&gt; xSeries2 = new ArrayList&lt;Double&gt;();
final List&lt;Double&gt; ySeries2 = new ArrayList&lt;Double&gt;();
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 &lt; multipleXseries.size(); i++) {
Plot.Data data = Plot.data().xy(multipleXseries.get(i), multipleYseries.get(i));
plot = plot.series(&quot;Data Series &quot; + i, data, null);
}
try {
plot.save(&quot;test&quot;, &quot;png&quot;);
} catch (IOException e) {
e.printStackTrace();
}
}
}

huangapple
  • 本文由 发表于 2020年10月1日 18:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64153632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定