> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# List Exercises

> Practice working with Lists in Dart through hands-on exercises

## Exercise Overview

This exercise demonstrates how to work with Lists in Dart using functions. You'll learn to:

* Create and populate lists with user input
* Pass lists to functions
* Iterate through lists and display their contents

## Problem Statement

Create a program that:

<Steps>
  <Step title="Create an empty list">
    Initialize an empty list of integers
  </Step>

  <Step title="Get user input">
    Use a function to ask the user for 10 integer values and add them to the list
  </Step>

  <Step title="Display the list">
    Use another function to print each element of the list
  </Step>
</Steps>

## Solution Approach

### Function 1: Getting User Input

This function receives a list, populates it with 10 user-provided integers, and returns the modified list:

```dart theme={null}
import 'dart:io';

List<int> pedirNumeros(List<int> numeros){
  for (var i = 0; i < 10; i++) {
    print('Introducir un Valor Entero:');
    numeros.add(int.parse(stdin.readLineSync()!));
  }
  return numeros;
}
```

<Note>
  When you pass a list to a function in Dart, you're passing a reference to the list. This means modifications inside the function affect the original list.
</Note>

### Function 2: Displaying the List

This function iterates through the list and prints each value:

```dart theme={null}
void imprimirLista(List<int> numeros){
  for (var numero in numeros) {
    print('El Valor es: $numero');
  }
}
```

<Tip>
  The `for-in` loop is perfect for iterating through all elements in a collection when you don't need the index.
</Tip>

### Main Function

The main function coordinates the program flow:

```dart theme={null}
void main() {
  List<int> numeros = [];
  pedirNumeros(numeros);
  imprimirLista(numeros);
}
```

## Complete Program

```dart theme={null}
/* Hacer un programa que empleando funciones cree una lista de números enteros y la cargue con 10 valores enteros dados por el usuario.
Ya con la lista cargada imprima cada uno de los elementos de dicha lista.*/

import 'dart:io';

List<int> pedirNumeros(List<int> numeros){
  for (var i = 0; i < 10; i++) {
    print('Introducir un Valor Entero:');
    numeros.add(int.parse(stdin.readLineSync()!));
  }
  return numeros;
}

void imprimirLista(List<int> numeros){
  for (var numero in numeros) {
    print('El Valor es: $numero');
  }
}

void main() {
  List<int> numeros = [];
  pedirNumeros(numeros);
  imprimirLista(numeros);
}
```

## Key Concepts

<CardGroup cols={2}>
  <Card title="List Declaration" icon="rectangle-list">
    `List<int> numeros = []` creates an empty list of integers
  </Card>

  <Card title="Adding Elements" icon="plus">
    Use `.add()` method to append elements to a list
  </Card>

  <Card title="For-In Loop" icon="arrows-rotate">
    `for (var item in list)` iterates through each element
  </Card>

  <Card title="Type Safety" icon="shield">
    `List<int>` ensures only integers can be added to the list
  </Card>
</CardGroup>

## List Operations Explained

<AccordionGroup>
  <Accordion title="Why return the list if it's already modified?">
    In Dart, lists are reference types. When you pass a list to a function and modify it, the changes affect the original list. Returning the list is optional but makes the function's behavior more explicit and allows for method chaining.

    ```dart theme={null}
    // Both work the same way
    pedirNumeros(numeros);
    // or
    numeros = pedirNumeros(numeros);
    ```
  </Accordion>

  <Accordion title="What's the difference between for and for-in?">
    **Regular for loop:**

    ```dart theme={null}
    for (var i = 0; i < numeros.length; i++) {
      print(numeros[i]);
    }
    ```

    Use when you need the index.

    **For-in loop:**

    ```dart theme={null}
    for (var numero in numeros) {
      print(numero);
    }
    ```

    Use when you only need the values.
  </Accordion>

  <Accordion title="How to handle invalid input?">
    Add error handling to make the program more robust:

    ```dart theme={null}
    List<int> pedirNumeros(List<int> numeros){
      for (var i = 0; i < 10; i++) {
        print('Introducir un Valor Entero:');
        try {
          numeros.add(int.parse(stdin.readLineSync()!));
        } catch (e) {
          print('Error: Introduce un número válido');
          i--; // Retry this iteration
        }
      }
      return numeros;
    }
    ```
  </Accordion>
</AccordionGroup>

## Enhanced Version

Here's an enhanced version with additional features:

```dart theme={null}
import 'dart:io';

List<int> pedirNumeros(List<int> numeros, int cantidad){
  for (var i = 0; i < cantidad; i++) {
    print('Introducir valor ${i + 1} de $cantidad:');
    numeros.add(int.parse(stdin.readLineSync()!));
  }
  return numeros;
}

void imprimirLista(List<int> numeros){
  print('\n=== Lista de Números ===');
  for (var i = 0; i < numeros.length; i++) {
    print('Posición $i: ${numeros[i]}');
  }
  print('\nTotal de elementos: ${numeros.length}');
}

void main() {
  List<int> numeros = [];
  pedirNumeros(numeros, 10);
  imprimirLista(numeros);
}
```

## Practice Challenges

<CardGroup cols={2}>
  <Card title="Challenge 1" icon="1">
    Modify the program to also calculate and display the sum and average of all numbers entered.
  </Card>

  <Card title="Challenge 2" icon="2">
    Add a function to find and display the maximum and minimum values in the list.
  </Card>

  <Card title="Challenge 3" icon="3">
    Create a function to sort the list in ascending order before printing.
  </Card>

  <Card title="Challenge 4" icon="4">
    Add a function to remove duplicate values from the list.
  </Card>
</CardGroup>
