Home > Blockchain >  Flutter/Dart Nested For & While loop behaving wiredly
Flutter/Dart Nested For & While loop behaving wiredly

Time:09-06

I have a use case where I am using a nested For loop to make a new list from the internal loop and add its value to another list using the outer loop. Here is the code:

For loop version:

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 loop version output:

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 loop version:

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 loop version output:

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

The same code I wrote in python seems to be working fine:

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)

The output of the python code:

[[4, 5, 6]]
[[4, 5, 6], [4, 5, 6, 1]]
[[4, 5, 6], [4, 5, 6, 1], [4, 5, 6, 1, 2]]

CodePudding user response:

Your First problem is that you are not reseting tempList in every main loop, and second one is When you one copy a list by value you should use .addAll, Try this:

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");
    }
  }

The output:

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]]
  • Related