- Forward


Segmented (or Packed) Arrays
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Idea:
    • Sometimes the records and fields (or rows and columns) are all of the same type allowing one array to be used instead of conformal arrays
  • An Example:
    • Data on the heights and weights of a variety of different people
Review
Back SMYC Forward
  • The Original Data:
    • javaexamples/programmingpatterns/segmentedarrays/SegmentedArrays.java (Fragment: data)
       
  • An Observation:
    • One array could be used to hold all of the elements
Thinking About the Problem
Back SMYC Forward

Concatenating the Two Arrays

javaexamples/programmingpatterns/segmentedarrays/SegmentedArrays.java (Fragment: concatenate2)
 
Thinking About the Problem (cont.)
Back SMYC Forward

Conceptualizing the Concatenated Array

SegmentedArrays_Concatenated
Thinking About the Problem (cont.)
Back SMYC Forward

Interleaving the Two Arrays

javaexamples/programmingpatterns/segmentedarrays/SegmentedArrays.java (Fragment: interleave)
 
Thinking About the Problem (cont.)
Back SMYC Forward

Conceptualizing the Interleaved Array

SegmentedArrays_PersonArray
The Pattern
Back SMYC Forward
  • From record and field to index:
    • javaexamples/programmingpatterns/segmentedarrays/SegmentedArrays.java (Fragment: toIndex)
       
  • From index to record and field:
    • javaexamples/programmingpatterns/segmentedarrays/SegmentedArrays.java (Fragment: fromIndex)
       
An Example
Back SMYC Forward
  • Recall:
    • The main() method of a Java application has a single String[] parameter (commonly named args)
    • When an application is executed from the command line, all of the String objects after the name of the main class are passed to the main() method using this array
  • Making Use of the Command Line Arguments:
    • Pass heights and weights into main()
An Example (cont.)
Back SMYC Forward
  • Finding the Indexes:
    • javaexamples/programmingpatterns/segmentedarrays/SegmentedArrays.java (Fragment: method)
       
  • Converting from String Objects to double Values:
    • javaexamples/programmingpatterns/segmentedarrays/SegmentedArrays.java (Fragment: args3)
       
There's Always More to Learn
Back -