Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In Dart programming, the List data type is similar to arrays in other programming languages. Lists are used to represent collections of objects. It’s an ordered set of objects. The core library in Dart is responsible for the existence, creation, and manipulation of the List class. Logical representation of a list:

The index of the element represents the location of a particular data and is displayed when a list item of the index is called. Typically, list items are invoked from their index. List types – There are roughly two types of lists, depending on their length:

  1. Fixed length list
  2. Growing list of

Fixed length list

In this case, the size of the list is originally declared and cannot be changed at run time. Syntax:

void main()
{
    var gfg = new List(3);
    gfg[0] = 'Geeks';
    gfg[1] = 'For';
    gfg[2] = 'Geeks';
​
    // Printing all the values in List
    print(gfg);
​
    // Printing value at specific position
    print(gfg[2]);
}
​
Copy the code

Add multiple values to the growable list —

void main() { var gfg = [ 'Geeks' ]; // Printing all the values in List print(gfg); // Adding multiple values in List and printing it // list_name.addAll([val 1, val 2, ...] ); gfg.addAll([ 'For', 'Geeks' ]); print(gfg); }Copy the code

Adds a value – to the growable list at a particular index

void main()
{
    var gfg = [ 'Geeks', 'Geeks' ];
​
    // Printing all the values in List
    print(gfg);
​
    // Adding new value in List at specific index and printing it
​
    // list_name.insert(index, value);
    gfg.insert(1, 'For');
    print(gfg);
​
Copy the code

Add multiple values to the growable list at a particular index –

void main()
{
    var gfg = [ 'Geeks' ];
​
    // Printing all the values in List
    print(gfg);
​
    // Adding new value in List at specific index and printing it
​
    // list_name.insertAll(index, list_of_values);
    gfg.insertAll(1, [ 'For', 'Geeks' ]);
    print(gfg);
​
    // Element at index 1 in list
    print(gfg[1]);
}
​
Copy the code

List types (their dimensional basis) : There are a number of dimensional-based lists, but the most popular are:

  1. One-dimensional (1-D) list
  2. Two dimensional (2-D) list
  3. A 3-dimensional (3-D) list
  4. Multidimensional list

Here, we have discussed 1-D lists.

Two-dimensional (2-D) list —

Here, the list is defined in two dimensions, giving it the appearance of a table. Create a two-dimensional list –

void main()
{
    int a = 3;
    int b = 3;
​
    // Creating two dimensional list
    var gfg = List.generate(a, (i) = > List(b), growable: false);
​
    // Printing its value
    print(gfg);
​
    // Inserting values
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            gfg[i][j] = i + j;
        }
    }
    // Printing its value
    print(gfg);
}
​
Copy the code

Another way to create a two-dimensional list –

void main()
{
    // Creating three dimensional list
    var gfg = List.generate(3, (i) = > List.generate(3, (j) = > i + j));
​
    // Printing its value
    print(gfg);
}
​
Copy the code

There is another way to create a two-dimensional list, which is to give the values associated with the index, which will result in the creation of a two-dimensional list.

3D (3-D) list —

Representation of a 3-D list is difficult, but its creation is similar to a 2-D list. Example:

void main()
{
    // Creating three dimensional list
    var gfg = List.generate(3, (i) = > List.generate(3,
                            (j) = > List.generate(3,
                            (k) = > i + j + k)));
​
    // Printing its value
    print(gfg);
}
​
Copy the code

Note: N-dimensional lists can be created in a similar way, using the “list.generate ()” method.

\