How to resolve the algorithm Associative array/Creation step by step in the Dart programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Associative array/Creation step by step in the Dart programming language
Table of Contents
Problem Statement
The goal is to create an associative array (also known as a dictionary, map, or hash).
Related tasks:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Associative array/Creation step by step in the Dart programming language
Source code in the dart programming language
main() {
var rosettaCode = { // Type is inferred to be Map<String, String>
'task': 'Associative Array Creation'
};
rosettaCode['language'] = 'Dart';
// The update function can be used to update a key using a callback
rosettaCode.update( 'is fun', // Key to update
(value) => "i don't know", // New value to use if key is present
ifAbsent: () => 'yes!' // Value to use if key is absent
);
assert( rosettaCode.toString() == '{task: Associative Array Creation, language: Dart, is fun: yes!}');
// If we type the Map with dynamic keys and values, it is like a JavaScript object
Map<dynamic, dynamic> jsObject = {
'key': 'value',
1: 2,
1.5: [ 'more', 'stuff' ],
#doStuff: () => print('doing stuff!') // #doStuff is a symbol, only one instance of this exists in the program. Would be :doStuff in Ruby
};
print( jsObject['key'] );
print( jsObject[1] );
for ( var value in jsObject[1.5] )
print('item: $value');
jsObject[ #doStuff ](); // Calling the function
print('\nKey types:');
jsObject.keys.forEach( (key) => print( key.runtimeType ) );
}
You may also check:How to resolve the algorithm 100 doors step by step in the BlitzMax programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Tcl programming language
You may also check:How to resolve the algorithm Infinity step by step in the NS-HUBASIC programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the XBS programming language