Lecture06 - CS162: Introduction to Computer Science II...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS162: Introduction to Computer Science II Arrays, ArrayLists, Exceptions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Arrays Data elements of the same type (primitive or Object reference) Access method is indexing Fixed length Get array length as data.length . ( Not a method !)
Background image of page 2
3 Array Example double [] data = new double [4]; data[3] = 2.5; double[] 0.0 0.0 0.0 2.5 data [0] [1] [2] [3] Length of array is 4 but highest index is 3 (ie. data.length-1)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Array Example double [] data = new double [4]; data[3] = 2.5; double[] 0.0 0.0 0.0 2.5 data [0] [1] [2] [3] Accessing data[4] results in an exception being thrown
Background image of page 4
5 Arrays as Parameters An entire array can be passed as a parameter to a method The reference to the array is passed (like any other object) formal and actual parameters are aliases of each other Changing an array element within the method changes the original
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Array elements as Parameters An array element can be passed to a method follows the parameter passing rules of that element's type eg. System.out.println(data[4]);
Background image of page 6
7 Copying Arrays Copying an array reference yields a second reference to the same array (shallow copy ) double [] data = new double [10]; // fill data array . . . double [] prices = data;
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Copying Arrays To make a true copy (deep copy ), each element must be copied double [] data = new double [10]; // fill data array . . . double [] prices = new double [10]; // copy elements of data array to prices array for ( int k = 0; k < 10; k++) prices[k] = data[k];
Background image of page 8
9 Partially Filled Arrays Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming convention: final int DATA_LENGTH = 100; double [] data = new double [DATA_LENGTH]; int dataSize = 0; Update dataSize as array is filled: data[dataSize] = x; dataSize++; Note dataSize is usually not equal to DATA_LENGTH
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Partially Filled Arrays
Background image of page 10
11 Partially Filled Arrays Remember to stop at dataSize when looking at array elements: for ( int i = 0; i < dataSize; i++) sum = sum + data[i]; Be careful not to overfill the array if (dataSize >= data.length) System.out.println("Sorry--array full");
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Partially Filled Arrays … or, you can "grow" the array: double[] newData = new double [2 * data.length]; for ( int k = 0; k < data.length; k++) newData[k] = data[k]; data = newData;
Background image of page 12
13 Growing an Array
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 Two-Dimensional Arrays For example, a tic-tac-toe board X O final int ROWS = 3; final int COLUMNS = 3; String[][] board = new String[ROWS][COLUMNS];
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 43

Lecture06 - CS162: Introduction to Computer Science II...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online