【问题标题】:How to automate function according to Array list size如何根据数组列表大小自动化功能
【发布时间】:2020-04-06 06:00:13
【问题描述】:

很抱歉,这个问题的标题不是 100% 正确的。因此,我将在这里解释我的情况。 我创建了一个函数来将 4 个数据集合并为一种返回格式。因为这是前端需要的格式。所以现在可以正常工作了。

public ReturnFormat makeThribleLineChart(List<NameCountModel> totalCount, List<NameCountModel>,p1Count, List<NameCountModel> p2Count, List<NameCountModel> average) {

        ReturnFormat returnFormat = new ReturnFormat(null,null);

            try {

                String[] totalData = new String[totalCount.size()];
                String[] p1Data = new String[p1Count.size()];
                String[] p2Data = new String[p2Count.size()];
                String[] averageData = new String[p2Count.size()];
                String[] lableList = new String[totalCount.size()];

                for (int x = 0; x < totalCount.size(); x++) {
                    totalData[x] = totalCount.get(x).getCount();
                    p1Data[x] = p1Count.get(x).getCount();
                    p2Data[x] = p2Count.get(x).getCount();
                    averageData[x] = average.get(x).getCount();
                    lableList[x] = totalCount.get(x).getName();
                }

                FormatHelper<String[]> totalFormatHelper= new FormatHelper<String[]>();
                totalFormatHelper.setData(totalData);
                totalFormatHelper.setType("line");
                totalFormatHelper.setLabel("Uudet");
                totalFormatHelper.setyAxisID("y-axis-1");

                FormatHelper<String[]> p1FormatHelper= new FormatHelper<String[]>();
                p1FormatHelper.setData(p1Data);
                p1FormatHelper.setType("line");
                p1FormatHelper.setLabel("P1 päivystykseen heti");

                FormatHelper<String[]> p2FormatHelper= new FormatHelper<String[]>();
                p2FormatHelper.setData(p2Data);
                p2FormatHelper.setType("line");
                p2FormatHelper.setLabel("P2 päivystykseen muttei yöllä");

                FormatHelper<String[]> averageFormatHelper= new FormatHelper<String[]>();
                averageFormatHelper.setData(averageData);
                averageFormatHelper.setType("line");
                averageFormatHelper.setLabel("Jonotusaika keskiarvo");
                averageFormatHelper.setyAxisID("y-axis-2");

                List<FormatHelper<String[]>> formatHelpObj = new ArrayList<FormatHelper<String[]>>();
                formatHelpObj.add(totalFormatHelper);
                formatHelpObj.add(p1FormatHelper);
                formatHelpObj.add(p2FormatHelper);
                formatHelpObj.add(averageFormatHelper);

                returnFormat.setData(formatHelpObj);
                returnFormat.setLabels(lableList);
                returnFormat.setMessage(Messages.Success);
                returnFormat.setStatus(ReturnFormat.Status.SUCCESS);


            } catch (Exception e) {

                returnFormat.setData(null);
                returnFormat.setMessage(Messages.InternalServerError);
                returnFormat.setStatus(ReturnFormat.Status.ERROR);

            }
            return returnFormat;

    }

所以,正如您在此处看到的,所有格式都是硬编码的。所以我的问题是如何自动化此代码以进行列表计数。假设下次我必须为五个数据集创建图表格式。所以我必须为它创建另一个函数。这就是我想要减少的东西。所以我希望你能理解我的问题。

谢谢。

【问题讨论】:

  • 看起来你的每个数据集都有几个属性——数据本身、标签和类型——对吗?如果您需要概括这一点,我建议考虑使用builder pattern
  • 不,所有数据集都具有相同的属性。数据集仅包含两个属性。 “名称”和“计数”。这意味着名称是标签,计数是数据。在上面的代码中,我将 4 个数据集传递给了参数列表。我打算将这些数据集作为单个列表传递。然后我希望循环该列表并为每个数据集创建一个单独的字符串数组。但我的问题是,我无法访问这些变量,因为它们在循环范围内。希望你能理解。

标签: java arraylist collections formatting


【解决方案1】:

您正在尝试解决基于动态信息组合结果对象(在本例中为 ReturnFormat)的更普遍的问题。此外,还有一些元数据与每个数据集一起设置 - 类型、标签等。在您发布的示例中,您已经硬编码了数据集和此元数据之间的关系,但您需要一些方法来如果您在此处有可变数量的参数,则为数据动态建立这种关系。

因此,您有两种选择:

  • makeThribleLineChart 设为varargs method 以接受代表数据的可变数量的参数。现在您遇到了将元数据与参数相关联的问题 - 最好的选择可能是将数据和元数据一起包装在某个新对象中,该对象作为 makeThribleLineChart 的每个参数提供。
    所以你最终会得到一个看起来有点像ReturnFormat makeThribleLineChart(DataMetadataWrapper... allDatasets) 的签名,其中DataMetadataWrapper 包含构建一个FormatHelper 实例所需的所有内容。
  • 使用类似于collection builders in guava 的构建器模式,例如:

class ThribbleLineChartBuilder {
    List<FormatHelper<String[]>> formatHelpObj = new ArrayList<>();

    ThribbleLineChartBuilder addDataSet(String describeType, String label, String yAxisId, List<NameCountModel> data) {
        String[] dataArray = ... ; // build your array of data

        FormatHelper<String[]> formatHelper = new FormatHelper<String[]>();
        formatHelper.setData(dataArray);
        formatHelper.setType(describeType);
        ... // set any other parameters that the FormatHelper requires here

        formatHelpObj.add(formatHelper);

        return this;
    }

    ReturnFormat build() {
        ReturnFormat returnFormat = new ReturnFormat(null, null);
        returnFormat.setData(this.formatHelpObj);
        ... // setup any other fields you need in ReturnFormat

        return returnFormat;
    }
}

// usage:
new ThribbleLineChartBuilder()
 .addDataSet("line", "Uudet", "y-axis-1", totalCount)
 .addDataSet("line", "P1 päivystykseen heti", null, p1Count)
 ... // setup your other data sources
 .build()

【讨论】:

  • 谢谢,帮了大忙。
猜你喜欢
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 2020-02-01
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多