Math107Ch6 - Introduction To Scientific Programming Chapter...

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

View Full Document Right Arrow Icon
Introduction To Scientific Programming Chapter 6 – Arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
S.Horton/107/Ch. 6 Slide 2 Lecture Overview on Arrays I. Array Basics I. Arrays in Classes and Methods I. I. Multidimensional Arrays
Background image of page 2
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”
Background image of page 3

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

View Full DocumentRight Arrow Icon
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];
Background image of page 4
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]);
Background image of page 5

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

View Full DocumentRight Arrow Icon
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).
Background image of page 6
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)
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online