Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Loop through a list of elements

Tags:

dart

My question is very simple, how can i create a loop that will loop a simple list of elements.

List li=["-","\\","|","/"];

this is my dart list and i want to create this simple animation.

like image 879
Sachihiro Avatar asked Mar 27 '18 13:03

Sachihiro


People also ask

Can you use a for loop for a list?

You can use a for loop to create a list of elements in three steps: Instantiate an empty list. Loop over an iterable or range of elements. Append each element to the end of the list.

Can you loop through a list in Java?

Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as an index to access each element.

How do you print a list of elements in a for loop in Python?

You can also loop through a list using a for loop in conjunction with the built-in Python range() method: for element in range(len(my_list)): print(f"I bought {my_list[element]}!")


2 Answers

Different ways to Loop through a List of elements

1 classic For

for (var i = 0; i < li.length; i++) {
  // TO DO
  var currentElement = li[i];
}

2 Enhanced For loop

for(final e in li){
  //
  var currentElement = e;
}

Notice the keyword final. It means single-assignment, a final variable's value cannot be changed.

3 while loop

var i = 0;
while(i < li.length){
  var currentElement = li[i];
  i++;
}

For the while loop, you will use var to reassign the variable value.

like image 164
Raymond Chenon Avatar answered Sep 21 '22 04:09

Raymond Chenon


there are may methods to get a loop from list for example we have this type of list

var mylist = [6, 7 ,9 ,8];

using for each method

 mylist.forEach((e){
   print(e);
 });

using for Loop

for (int i=0 ; i<mylist.length; i++){
   var e = mylist[i];
  print(e);
}

Enhanced For loop

for (var e in mylist){
   print(e);
}

using while loop

 var i = 0;
  while(i < mylist.length){
    print(mylist[i]);
   i++;
 }
like image 35
Akbar Masterpadi Avatar answered Sep 17 '22 04:09

Akbar Masterpadi