RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

arrays: construct vs init
acronyms

In today's lecture, we saw how to declare arrays of any size,

  1. Together we will look at the following results, in BlueJ, and draw pictures about wht is happening:
    double[] nums;
    double[] data;
    
    nums = new double[]{3,4,5};
    
    data = new double[12];
    data[3] = 33.3;
    data[4] = 44.4;
    data[3] = Math.sqrt(3);
    
    int i;
    while (i < Object120.length(nums2)) {
        data[i] = Math.sqrt(i);
        i += 1;
    }
          
  2. Bonus-tip: for loops:
    for (int i=0;  i < Object120.length(data);  i += 1) {
        data[i] = 2*Math.sqrt(i);
        }
    
    The general syntax:
    for ( var-init ; test ; end-of-loop-update ) {
        body
        
    }
    
    The advantage of a for-loop: a single line has the init, test, and update of the loop-control variable, all in one convenient location! (It was in three different places, for a while loop.)
  3. Complete the two indicated methods in ArrayAllocate.java.
  4. Submit your file to D2L, as lab07b.
  5. Challenge problem: Write a function appendPairs, which takes an array of strings, and returns a new array of each item joined with it's successor (except for the last element, which is that element joined with the first):
            assertEquals( new String[]{"really-no","no-bye","bye-really"}, appendPairs(new String[]{"really","no","bye"}) );     
          
    You might well have a special-case to handle the last entry; should it be inside the loop or not?
  6. Challenge, continued: Can you use % to not make the last location a special-case?!

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.