Associative Arrays Solutions - D Programming Language
Description: D Programming Language
Keywords: D programming language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# Associative Arrays Solutions

#### Associative Arrays Dersi Problem Çözümleri

1. - The `.keys` property returns a slice (i.e. dynamic array) that includes all of the keys of the associative array. Iterating over this slice and removing the element for each key by calling `.remove` would result in an empty associative array:

         import std.stdio;

         void main() {
             string[int] names =
             [
                 1   : "one",
                 10  : "ten",
                 100 : "hundred",
             ];

             writeln("Initial length: ", names.length);

             int[] keys = names.keys;

             /* 'foreach' is similar but superior to 'for'. We will
              * see the 'foreach' loop in the next chapter. */
             foreach (key; keys) {
                 writefln("Removing the element %s", key);
                 names.remove(key);
             }

             writeln("Final length: ", names.length);
         }

     That solution may be slow especially for large arrays. The following methods would empty the array in a single step.

   - Another solution is to assign an empty array:

             string[int] emptyAA;
             names = emptyAA;

   - Since the initial value of an array is an empty array anyway, the following technique would achieve the same result:

             names = names.init;

2. The goal is to store multiple grades per student. Since multiple grades can be stored in a dynamic array, an associative array that maps from `string` to `int[]` would work here. The grades can be appended to the dynamic arrays that are stored in the associative array:

       import std.stdio;

       void main() {
           /* The key type of this associative array is string and
            * the value type is int[], i.e. an array of ints. The
            * associative array is being defined with an extra
            * space in between to help distinguish the value type: */
           int[] [string] grades;

           /* The array of ints that correspond to "emre" is being
            * used for appending the new grade to that array: */
           grades["emre"] ~= 90;
           grades["emre"] ~= 85;

           /* Printing the grades of "emre": */
           writeln(grades["emre"]);
       }

   The grades can also be assigned in one go with an array literal:

       import std.stdio;

       void main() {
           int[][string] grades;

           grades["emre"] = [ 90, 85, 95 ];

           writeln(grades["emre"]);
       }

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Mon Aug 17 18:57:13 2026