【问题标题】:How to Insert For In Loop Variable into Variable Call如何将 For In 循环变量插入到变量调用中
【发布时间】:2020-10-24 14:49:23
【问题描述】:
    class GraphData {
    List <double> closingPricesBTC = [];
    List <double> closingPricesETH = [];
    List <double> closingPricesLTC = [];

        for (String cryptoCurrency in cryptoAbbreviation){
        .
        . (some code)
        .
        double closePrice = ....

        closingPrices.add(closePrice);
        }

}

cryptAbbreviation 在哪里

const List<String> cryptoAbbreviation = ['BTC', 'ETH', 'LTC'];

如何将当前的“加密货币”字符串附加到 closingPrices.add(closePrice) 的变量名称中,以便最终得到 closingPricesBTC.add(closePrice)closingPricesETH.add(closePrice)closingPricesLTC.add(closePrice)

我尝试过closingPrices$cryptoCurrency.add(closePrice);,但这不起作用。

【问题讨论】:

  • 在 Java 中,您不能在运行时即时“编造”变量名。
  • 它在颤振中也不起作用
  • 这里要做的是将您的List&lt;Double&gt;s(不是 List&lt;double&gt;)放在Map&lt;String, List&lt;Double&gt;&gt; 中,并以缩写作为键。
  • @KevinAnderson,是的,我在写同样的答案

标签: java flutter variables naming


【解决方案1】:

您不能生成变量名,但可以尝试 HashMap(Java)/Map(Dart)。这应该满足您的要求。

Map<String, List<double>> closingPrices = {'BTC':[], 'ETH':[], 'LTC':[]};


        for (String cryptoCurrency in cryptoAbbreviation){
        .
        . (some code)
        .
        double closePrice = ....

        closingPrices[cryptoCurrency].add(closePrice);
        }

【讨论】:

  • 感谢您的回答。我会尝试这个并报告回来让你知道这是否适合我。
猜你喜欢
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 2021-01-20
  • 2016-07-05
相关资源
最近更新 更多