A function that dynamically prints all the elements of the list in reverse order [1, 2, 3, 5, 8, 13, 21, 34, 55, 89],

Asked

Viewed 34 times

-3

DART FUNCTION TO PRINT ELEMENTS IN REVERSE ORDER IN CASE SHE WOULD BE LIKE THIS 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 89,55, 34, 21, 13, 8, 5, 3, 2, 1 REVERSE

  • Opa all right? Looks like you’re new around here, take a look at these topics Question without much quality and How to ask a good question.

  • By your username I believe you already know the Dartpad, correct? Did you know that there are some shortcuts in the editor? For example, create a property as follows List<int> lista = [1, 2, 3];, then in the bottom row type lista. and with the cursor in front of the "point" press CTRL+Space... You will see all the useful methods that such a type offers you, with this you will find what you want...

1 answer

0


Statement for serves to repeat processes until the declared variable reaches the stated limit.

void main() {
    List<int> lista = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89];

    for (int i = lista.length; i > 0; i--) {
        print(lista[i - 1]);
    }

}

// Saída: 89 55 34 21 13 8 5 3 2 1

See that we started the variable i with the value of the quantities of elements that the vector lista possesses (i = lista.lenght);

Next, we define until when the for repeat, in case it will repeat while i is greater than 0 (i > 0);

And finally, we define that with each repetition of the for, the value of i is subtracted in a (i--or i = i - 1).

The same principle can be used to increase, but reverses the values:

void main() {
    List<int> lista = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
    for (int i = 0; i < lista.length; i++) {
        print(lista[i]);
    }
}

// Saída: 1 2 3 5 8 13 21 34 55 89
  • Your answer is legal, but there is already a proper function for it in the types List<T> that in a row you solve the problem of reversing a list.

  • The problem is that the issue is not mentioned, but this seems to be an academic issue, and if you’re starting at Dart it’s good to take the basic principles of looping as well

  • 1

    The point is that the AP has given us no example of what it has tried so far and played a generic "question" and anyway, which as you said "Sounds like an academic question" and probably is, because there is another identical question of his that was closed by not meet the minimum criteria of quality of the site . I advise you to read Manual on how NOT to ask questions.

  • Thank you @Jeanfranz for the tip.. I’m actually having my first contacts with Dart, and I got on this platform recently, looking for help, and I’m sorry I didn’t come up with a better question.. I am still learning how to use this platform!! and I confess that I have received many abusive comments here.. I thought this site was for students tbm, but the visa only has doctors.. I thank you for answering my question worked well ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.