我有一個用例,我使用嵌套的 For 回圈從內部回圈創建一個新串列,并使用外部回圈將其值添加到另一個串列中。這是代碼:
對于回圈版本:
void main(List<String> arguments) {
print('Hello world!');
List sortedMapList = [1, 2, 3];
List pickupPolyLineList = [4, 5, 6];
List tempList = [];
List wayPointsList = [];
for (int i = 0; i <= sortedMapList.length; i ) {
tempList = pickupPolyLineList;
for (int j = 0; j < i; j ) {
tempList.add(sortedMapList[j]);
// print("tempList at internal index j: $j is: $tempList");
}
print(" Before wayPointsList at internal index i: $i is: $wayPointsList \n");
wayPointsList.add(tempList);
print(" after wayPointsList at internal index i: $i is: $wayPointsList");
}
}
For回圈版本輸出:
Hello world!
Before wayPointsList at internal index i: 0 is: []
after wayPointsList at internal index i: 0 is: [[4, 5, 6]]
Before wayPointsList at internal index i: 1 is: [[4, 5, 6, 1]]
after wayPointsList at internal index i: 1 is: [[4, 5, 6, 1], [4, 5, 6, 1]]
Before wayPointsList at internal index i: 2 is: [[4, 5, 6, 1, 1, 2], [4, 5, 6, 1, 1, 2]]
after wayPointsList at internal index i: 2 is: [[4, 5, 6, 1, 1, 2], [4, 5, 6, 1, 1, 2], [4, 5, 6, 1, 1, 2]]
While回圈版本:
void main(List<String> arguments) {
print('Hello world!');
List sortedMapList = [1, 2, 3];
List pickupPolyLineList = [4, 5, 6];
List tempList = [];
List wayPointsList = [];
int i = 0;
int j = 0;
while (i < sortedMapList.length) {
tempList = pickupPolyLineList;
while (j < i) {
tempList.add(sortedMapList[j]);
// print("tempList at internal index j: $j is: $tempList");
j ;
}
print("tempList at internal index is: $tempList");
print(" Before wayPointsList at internal index i: $i is: $wayPointsList");
wayPointsList.add(tempList);
print(" after wayPointsList at internal index i: $i is: $wayPointsList \n");
i ;
}
}
While 回圈版本輸出:
Hello world!
tempList at internal index is: [4, 5, 6]
Before wayPointsList at internal index i: 0 is: []
after wayPointsList at internal index i: 0 is: [[4, 5, 6]]
tempList at internal index is: [4, 5, 6, 1]
Before wayPointsList at internal index i: 1 is: [[4, 5, 6, 1]]
after wayPointsList at internal index i: 1 is: [[4, 5, 6, 1], [4, 5, 6, 1]]
tempList at internal index is: [4, 5, 6, 1, 2]
Before wayPointsList at internal index i: 2 is: [[4, 5, 6, 1, 2], [4, 5, 6, 1, 2]]
after wayPointsList at internal index i: 2 is: [[4, 5, 6, 1, 2], [4, 5, 6, 1, 2], [4, 5, 6, 1, 2]]
我在 Python 中撰寫的相同代碼似乎運行良好:
adj = [1, 2, 3]
fruits = [4, 5, 6]
fruits2 = []
tempList = []
for x in range(len(fruits)):
tempList = fruits[:]
for i in range(x):
tempList.append(adj[i])
fruits2.append(tempList)
print(fruits2)
python代碼的輸出:
[[4, 5, 6]]
[[4, 5, 6], [4, 5, 6, 1]]
[[4, 5, 6], [4, 5, 6, 1], [4, 5, 6, 1, 2]]
uj5u.com熱心網友回復:
您的第一個問題是您沒有tempList
在每個主回圈中重置,第二個問題是當您按值復制串列時應該使用.addAll
,試試這個:
void testy() {
print('Hello world!');
List sortedMapList = [1, 2, 3];
List pickupPolyLineList = [4, 5, 6];
List tempList = [];
List wayPointsList = [];
for (int i = 0; i <= sortedMapList.length; i ) {
tempList = [];// <--- add this
tempList.addAll(pickupPolyLineList);// <--- change this
for (int j = 0; j < i; j ) {
tempList.add(sortedMapList[j]);
// print("tempList at internal index j: $j is: $tempList");
}
print(
" Before wayPointsList at internal index i: $i is: $wayPointsList \n");
wayPointsList.add(tempList);
print(" after wayPointsList at internal index i: $i is: $wayPointsList");
}
}
輸出:
flutter: Hello world!
flutter: Before wayPointsList at internal index i: 0 is: []
flutter: after wayPointsList at internal index i: 0 is: [[4, 5, 6]]
flutter: Before wayPointsList at internal index i: 1 is: [[4, 5, 6]]
flutter: after wayPointsList at internal index i: 1 is: [[4, 5, 6], [4, 5, 6, 1]]
flutter: Before wayPointsList at internal index i: 2 is: [[4, 5, 6], [4, 5, 6, 1]]
flutter: after wayPointsList at internal index i: 2 is: [[4, 5, 6], [4, 5, 6, 1], [4, 5, 6, 1, 2]]
flutter: Before wayPointsList at internal index i: 3 is: [[4, 5, 6], [4, 5, 6, 1], [4, 5, 6, 1, 2]]
flutter: after wayPointsList at internal index i: 3 is: [[4, 5, 6], [4, 5, 6, 1], [4, 5, 6, 1, 2], [4, 5, 6, 1, 2, 3]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/505820.html
下一篇:如何實作“安全”的密碼插入?