Lecture 18, CS 302-6, October 14

 

  1. Reminders
    1. Exam 1 – October 20

                                                               i.      By end of class today – will have covered everything on exam

1.      no 2d arrays on exam

    1. Program 2 – October 28

                                                               i.      Pair programming – email me by EOD Saturday if you would like me to assign a partner for you to work with

  1. Review
    1. Variable scope
    2. Call stack tracing
    3. Arrays as parameters

                                                               i.      And return values

  1. Passing arrays
    1. You can also return arrays: (from last spring’s test)

                                                               i.      Example - PassingAndReturning.java

                                                             ii.      Call stack vs heap

1.      Call stack – where our methods go

2.      Heap – memory for storing arrays, etc

    1. Now, what is the difference between passing arrays and, say, integers?

                                                               i.      Example – ArrayPassing.java

                                                             ii.      The important concept here is the difference between data that is stored as a reference and data that is stored as a primitive.  Integers, doubles, chars, etc are primitives – you can think of the data is being stored in the variable.  Arrays, on the other hand, are stored as references – the variable itself only holds a pointer to the location in memory where the data is stored.  That pointer is just a number referencing an address in memory.

  1. 2d arrays
    1. Example – chess board (1-8 up/down, a-h left/right)

                                                               i.      (algebraic chess notation)

    1. How to declare:

                                                               i.      int[][] board=new int[8][8];

                                                             ii.      Or, if you know the values you want to put in it:

1.      int[][] board={{1,2,3},{4,5,6},{7,8,9}}

If we were to traverse and print this array, we might get something like:

1 2 3

4 5 6

7 8 9

  1. HW – study for exam