2.2.2 Representing Arrays in Object Diagrams

Representing Arrays in Object Diagrams

A Wildcats score array has indexed positions whose values can change. Draw the array at a chosen pause, not merely the initializer as originally written.

Read the Code Before Drawing

This excerpt runs inside an existing method. The diagram is taken after the correction to the second score.

If the underlying C# feature is unfamiliar, use the lesson below while reading this example.

Source to translate
int[] roundGoals = { 2, 0, 3 };
roundGoals[1] = 1;
Rewrite the Meaning in Ordinary Words
The same snapshot in ordinary words
Create one integer array with scores 2, 0, and 3
Replace the score at index 1 with 1
Pause after the replacement
The same three positions now contain 2, 1, and 3

These words fix the snapshot moment and separate instances, values, and reference targets. Follow the source to that pause before deciding how many boxes to draw and which current values belong inside them.

Map the Source to Object Boxes and References
Build the Diagram in Violet
  1. Create an object diagram in Violet with one object headed roundGoals : int[]. Keep the array type brackets in the header.
  2. Add indexed field-name rows [0], [1], and [2]. Enter current values 2, 1, and 3 beside them.
  3. Place a note beside the array reading Length = 3; snapshot after correction. Length counts positions, not the largest index.
  4. Do not draw Player boxes or reference arrows for these integer elements. The actual integers are stored in their positions.
  5. Save and point to [1] while reading roundGoals[1] from the code. It must show 1 at this pause, not the original zero.
Compare the Completed Soccer Diagram

Compare this reference with the diagram you built in Violet. Layout may differ; the source meaning, labels, and relevant connections should agree.

An integer array has three positions with values 2, 1, and 3 after index one is corrected.
One array after its middle score changes
  • New
  • Changed
  • Removed
Trace the Snapshot One Reference or Value at a Time

Follow roundGoals[1] to the row marked [1]. It contains the value 1, not the index number because they happen to match. The valid indexes remain 0, 1, and 2.

Verify the Drawing Against the Source
Predict and Check a Small Change