Lecture 19, CS 302-6, October 17

 

  1. Reminders
    1. Exam 1 – October 20

                                                               i.      no 2d arrays on exam

    1. Program 2 – October 28
    2. Office hours this week

                                                               i.      Monday afternoon (not 3-4)

                                                             ii.      Tuesday 12:30-2:30

                                                            iii.      Wednesday afternoon

                                                           iv.      Thursday 12:30-2:30, (exam 5-7)

                                                             v.      Review session – Tuesday, 5-7, CS 1257

  1. Review
    1. Call stack tracing
    2. Arrays as parameters and return values
    3. 2d arrays
  2. 2d+ arrays
    1. Definition – ‘Array of arrays’
    2. declaring

                                                               i.      [][] or {{,},{,},…}

    1. referencing element

                                                               i.      [I][j]

                                                             ii.      [row][column]

    1. Example – TicTacToe.java
    2. How to use

                                                               i.      traversing – with nested for loops

1.      Note – x[j] refers to an array itself if x is a 2 dimensional array

2.      So, we have x.length, x[I].length

a.       X.length is the number of rows in the entire array, x[I].length is the number of columns in the array stored at row i

3.      Note – 2d arrays can contain different numbers of rows and columns.

                                                             ii.      Example – TopicsChooser.java

  1. Review
    1. Order:

                                                               i.      Arrays

                                                             ii.      Methods

                                                            iii.      Stepwise Refinement, Call Stack Tracing

                                                           iv.      Scanner, random, etc

                                                             v.      Loops

                                                           vi.      Boolean Operators

                                                          vii.      Conditional Statements

                                                        viii.      Data Types

    1. Array primer

                                                               i.      Why arrays?  They allow us to hold multiple pieces of data in one variable

                                                             ii.      What is an array?  List of elements

1.      Each element can be referred to by an index

a.       Start counting at 0

2.      Size – number of elements

a.       .length

                                                            iii.      Declaring

1.      type[] name=new type[length]

a.       if we do this, we initialize each value to something

                                                                                                                                       i.      example – int[] initializes each value to 0

2.      type[] name={x1, x2,…,xi-1}, i is length

3.      Can be of any type: int, double, String, etc

                                                           iv.      Using

1.      name[index]

                                                             v.      Arrays as reference

1.      keep in mind – variables only store one value

a.       so, the variable for an array just stores a pointer to a memory location that stores the array

    1. Example 1 – ArrayParameters.java (from spring 2011 exam)
  1. HW – Prepare questions for Wednesday