CH06 - Chapter 6 Java: an Introduction to Computer Science...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 6 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 6 Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays Arrays Chapter 6 Java: an Introduction to Computer Science & Programming - Walter Savitch Overview An array: a single name for a collection of data values, all of the same data type subscript notation identifies precisely one of the values Arrays are a carryover from earlier programming languages Array: more than a primitive type, less than an object their methods are invoked with a special subscript notation most programmers do not even think of them as methods they work like objects when used as method arguments and return types they do not have or use inheritance they are sort of like a Java class that is not fully implemented Arrays are a natural fit for loops, especially for loops Chapter 6 Java: an Introduction to Computer Science & Programming - Walter Savitch 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 of double s: double reading = new double[100]; 80-element array of Species : Species specimen = new Species[100]; Chapter 6 Java: an Introduction to Computer Science & Programming - Walter Savitch Programming Tip : Use Singular Array Names Using singular rather than plural names for arrays improves readability Although the array contains many elements the most common use of the name will be with a subscript, which references a single value. Chapter 6 Java: an Introduction to Computer Science & Programming - Walter Savitch Three Ways to Use [ ] (Brackets) with an Array Name 1. To create a type name, e.g. int pressure; creates a name with the type " int array" note that the types int and int array are different it is the type of the name, not the type of the data 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]); Chapter 6 Java: an Introduction to Computer Science & Programming - Walter Savitch Some 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 variable- also called an element or subscripted variable Note that "element" may refer to either a single indexed variable in the array or the value of a single indexed variable....
View Full Document

Page1 / 51

CH06 - Chapter 6 Java: an Introduction to Computer Science...

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

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