{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Math107Ch6

# Math107Ch6 - Introduction To Scientific Programming Chapter...

This preview shows pages 1–9. Sign up to view the full content.

Introduction To Scientific Programming Chapter 6 – Arrays

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

View Full Document
S.Horton/107/Ch. 6 Slide 2 Lecture Overview on Arrays I. Array Basics I. Arrays in Classes and Methods I. I. Multidimensional Arrays
S.Horton/107/Ch. 6 Slide 3 I. Array Basics An array is a single name for a collection of data values (all of the same data type) A special subscript notation identifies precisely one of the values of the collection Arrays work like objects: They are initialized like objects There can be multiple arrays of the same type Arrays are a natural fit for loops, especially for loops However, they are not “full” objects No real methods, constructors, or “inheritance”

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

View Full Document
S.Horton/107/Ch. 6 Slide 4 Creating Arrays General syntax for declaring an array: Base_Type[] Array_Name = new Base_Type[Length]; Examples: 80-element array with base type char : char[] symbol = new char[80]; 100-element array with base type double : double[] reading = new double[100]; 100-element array with base type AddressBook : AddressBook[] homebook = new AddressBook[100];
S.Horton/107/Ch. 6 Slide 5 Three Uses of [ ] (Brackets) with an Array Name 1. To create an array type name, e.g. int[] pressure; creates a name with the type " int array" The types int and “ int array” are different 2. To create a new array, e.g. pressure = new int[100]; 3. To name a specific element in the array (also called an indexed variable) , e.g. pressure[3] = SavitchIn.readLineInt(); System.out.println("You entered" + pressure[3]);

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

View Full Document
S.Horton/107/Ch. 6 Slide 6 An Array Declaration Can Be “Broken-Up” Last slide syntax was correct! Why? Because you can break up declaration like any object For example, the complete declaration: int[] pressure = new int[100]; can be broken up into: int[] pressure; // first part declaration pressure = new int[100]; // second part assignment This can be handy for re-declaration (more on this soon).
S.Horton/107/Ch. 6 Slide 7 Array Terminology temperature [n + 2] temperature[ n + 2 ] temperature[n + 2] temperature[n + 2] = 32 ; Array name Index ( also called a subscript) - Must be an int , or an expression that evaluates to an int Indexed element/variable (also called a subscripted variable) Note: an "element" may refer to either a single indexed variable in the array or the value of a single indexed variable. Value of the indexed variable (also called an element of the array)

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

View Full Document
S.Horton/107/Ch. 6 Slide 8 Array Length The length of an array is specified by the number given in the brackets when it is created with new This determines the maximum number of elements the array can hold and the amount of memory allocated for the elements The array length can always be read with the instance variable length . E.g. the following code displays the number 20 (this is the
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 57

Math107Ch6 - Introduction To Scientific Programming Chapter...

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

View Full Document
Ask a homework question - tutors are online