动态创建列表时遇到一些麻烦。
例如,当我们以这种方式创建列表时...
List testList = [["John", 100], ["Maria", 200]]; // Some Strings and integers.
(要么)
testList.add(["John", 100]);
testList.add(["Maria", 200]);
...我们可以通过(使用打印)访问列表的信息和条目...
testList.length; // That returns: 2
testList; // That returns: [[John, 100], [Maria, 200]]
testList[0]; // That returns: [John, 100]
testList[1]; // That returns: [Maria, 200]
到目前为止,没有新内容(至少对我而言)。但是有一个有趣的地方,我无法通过一些动态代码进行重现(我将在后面解释)。
如果您尝试以某种多维列表/数组的形式访问该列表,则它将起作用。我没想到这一点,因为我读了很多教程说Dart不能原生处理这种列表/数组。让我们再次使用print看看...
testList[0]; // Again, that returns: [John, 100]
testList[1]; // Again, that returns: [Maria, 200]
testList[0][0]; // That returns: John
testList[0][1]; // That returns: 100
testList[1][0]; // That returns: Maria
testList[1][1]; // That returns: 200
实际上,这确实不错,但是现在在尝试动态创建这些多维列表时遇到一个问题。
我想通过从另一个列表中获取一些数据来创建一个动态对象列表(它就像我已经解释过的“testList”一样工作)。如果我知道要添加到新列表中的信息的确切数量,则可以这样:
newDynamicList.add([dataSourceList[0], dataSourceList[1]);
但是,这不是我的意图。我想建立一个可以动态执行的方法。换句话说,从数据源列表中获取多少参数,并根据相应的“行”将其添加([0] [0],[0] [1],[0] [2],...) 。
我正在尝试这样做:
// Where:
// dataList is the data source. Each line has an object.
// numberOfFields reflects how wide the new data List will get.
// iteration is a local variable that is compared to numberOfFields in order to
// let the code know when to finish that List's line and start a new one.
List getDataParameters(List dataList, int numberOfFields) {
List tempDataList = new List(); // This list will store the objects in order to build the dataParametersList.
List dataParametersList = new List(); // This list will be returned to the caller.
if (dataList.length != 0 && numberOfFields != 0) { // Check for null.
if (numberOfFields == 1) { // Just one field, do this.
for (int loop = 0; loop < dataList.length; loop++) {
dataParametersList.add(dataList[loop]);
}
} else { // More than one field.
int iteration = 1;
for (int loop = 0; loop < dataList.length; loop++) {
if (iteration < numberOfFields) {
tempDataList.add(dataList[loop]);
iteration++;
} else {
tempDataList.add(dataList[loop]);
dataParametersList.add(tempDataList); // >>> HERE LIES THE PROBLEM. <<<
iteration = 1;
}
}
}
} else {
// If there's no data, how to throw exception here?
}
return dataParametersList;
}
我假设“dataParametersList.add(tempDataList);”会将对象从tempDataList添加到dataParametersList中,就像我手动执行的那样。但这是行不通的。
因此,问题是:我如何创建这些动态对象像第一个示例一样能够使用它们的列表?
很长的问题,我很抱歉,但是我想尽量保持清楚。
编辑1:我忘了说我知道我提供的代码有一个错误(可能是更多原因):每次迭代都会将tempDataList上的所有信息添加到dataParametersList,因为我没有清除tempDataList(并清除只会使情况变得更糟,因为后面的列表将变为空)。
编辑2:
getDaraParameters(List dataList,int numberOfFields)用法示例:
List dtLst = new List();
dtLst.add("John");
dtLst.add(100);
dtLst.add("Maria");
dtLst.add(200);
print(dtLst); // Returns: [John, 100, Maria, 200]
我正在考虑使用两个字段,因此上面的4行列表在传递给getDataParameters(dtLst,2)后应该看起来像这样(假定打印其结果):
[[John, 100], [Maria, 200]]
但是我需要它像问题的第一个示例一样工作,多维坐标在这里工作。
最佳答案
还不是答案,但是无论如何:
代替
for (int loop = 0; loop < dataList.length; loop++) {
dataParametersList.add(dataList[loop]);
}
你可以写
dataParametersList.addAll(dataList);
更新
这应该做。
import 'dart:math' as math;
void main() {
print(getDataParameters([], 2));
print(getDataParameters(["John"], 2));
print(getDataParameters(["John", 100, "Maria"], 2));
print(getDataParameters(["John", 100, "Maria", 200], 2));
print(getDataParameters(["John", 100, "Maria", 200, 10], 2));
}
List getDataParameters(List dataList, int numberOfFields) {
var result = [];
int i = 0;
while(dataList.length > i) {
result.add(dataList.sublist(i, math.min(i + numberOfFields, dataList.length)));
i += numberOfFields;
}
return result;
}
输出
[]
[[John]]
[[John, 100], [Maria]]
[[John, 100], [Maria, 200]]
[[John, 100], [Maria, 200], [10]]
sublist
创建一个包含dataList[i]
到dataList[i + numberOfFields]
的项目的列表。此列表已添加到
result
中。math.min(i + numberOfFields, dataList.length)
可确保在dataList
的末尾仅请求剩余的项目(当dataList.length
不能被numberOfFields
除以剩余数时)。(当
dataList
有5个项目时,sublist(4,6)
将引发异常)较短的版本
List getDataParameters(List l, int f) {
return new List.generate((l.length / f).ceil(),
(i) => l.sublist(i * f, math.min(i * f + f, l.length)));
}
关于list - Dart语言:创建和访问动态列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25182780/