This task is about arrays.

For hashes or associative arrays, please see Creating an Associative Array.

For a definition and in-depth discussion of what an array is, see Array.



Task

Show basic array syntax in your language.

Basically, create an array, assign a value to it, and retrieve an element   (if available, show both fixed-length arrays and
dynamic arrays, pushing a value into it).

Please discuss at Village Pump:   Arrays.

Please merge code in from these obsolete tasks:

   Creating an Array
   Assigning Values to an Array
   Retrieving an Element of an Array


Related tasks

   Collections
   Creating an Associative Array
   Two-dimensional array (runtime)


 
main(){
	// Dart uses Lists which dynamically resize by default
	final growable = [ 1, 2, 3 ];
 
        // Add to the list using the add method
	growable.add(4);
 
	print('growable: $growable');
 
	// You can pass an int to the constructor to create a fixed sized List
	final fixed = List(3);
 
	// We must assign each element individually using the Subscript operator
	// using .add would through an error
	fixed[0] = 'one';
	fixed[1] = 'two';
	fixed[2] = 'three';
 
	print('fixed: $fixed');
 
	// If we want to create a fixed list all at once we can use the of constructor
	// Setting growable to false is what makes it fixed
	final fixed2 = List.of( [ 1.5, 2.5, 3.5 ], growable: false);
 
	print('fixed2: $fixed2');
 
	// A potential gotcha involving the subscript operator [] might surprise JavaScripters
	// One cannot add new elements to a List using the subscript operator
	// We can only assign to existing elements, even if the List is growable
 
	final gotcha = [ 1, 2 ];	
	// gotcha[2] = 3 would cause an error in Dart, but not in JavaScript
	// We must first add the new element using .add
	gotcha.add(3);
	// Now we can modify the existing elements with the subscript
	gotcha[2] = 4;
 
	print('gotcha: $gotcha');
 
 
}
 
 