DOC PREVIEW
Penn CIT 591 - Arrays

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ArraysA problem with simple variablesMultiple valuesIndexing into arraysUsing array elementsArray valuesStrings and arraysDeclaration versus definitionDeclaring and defining arraysArray assignmentAn array’s size is not part of its typeSlide 12Example of array use IExample of array use IIExample of array use IIIArray names IArray names IILength of an arrayMagic numbersNullPointerExceptionArrays of objectsInitializing arrays IInitializing arrays IIArray literalsInitializing arrays IIIArrays of arraysExample array of arraysSize of 2D arraysThe EndArraysA problem with simple variables•One variable holds one value–The value may change over time, but at any given time, a variable holds a single value•If you want to keep track of many values, you need many variables•All of these variables need to have names•What if you need to keep track of hundreds or thousands of values?Multiple values•An array lets you associate one name with a fixed (but possibly large) number of values•All values must have the same type•The values are distinguished by a numerical index between 0 and array size minus 112 43 6 83 14 -57 109 12 0 6 0 1 2 3 4 5 6 7 8 9myArrayIndexing into arrays•To reference a single array element, use array-name [ index ]•Indexed elements can be used just like simple variables–You can access their values–You can modify their values•An array index is sometimes called a subscriptUsing array elements•Examples:• x = myArray[1]; // sets x to 43• myArray[4] = 99; // replaces 14 with 99• m = 5; y = myArray[m]; // sets y to -57• z = myArray[myArray[9]]; // sets z to 10912 43 6 83 14 -57 109 12 0 6 0 1 2 3 4 5 6 7 8 9myArrayArray values•An array may hold any type of value•All values in an array must be the same type–For example, you can have:•an array of integers•an array of Strings•an array of Person•an array of arrays of String•an array of ObjectStrings and arrays•Strings and arrays both have special syntax•Strings are objects, and can be used as objects•Arrays are objects, but–Arrays are created using special syntax:new type[size] instead of new Person()–If an array holds elements of type T, then the array’s type is “array of T”Declaration versus definition•Arrays are objects•Creating arrays is like creating other objects:–the declaration only provides type information–the new definition actually allocates space–declaration and definition may be separate or combined•Example for ordinary objects: Person p; // declaration p = new Person("John"); // definition Person p = new Person("John"); // combinedDeclaring and defining arrays•Example for array objects:–int myArray[ ]; // declaration•This declares myArray to be an array of integers•Notice that the size is not part of the type–myArray = new int[10]; // definition•new int[10] creates the array•the rest is an ordinary assignment statement–int myArray[ ] = new int[10]; // bothArray assignment•Array assignment is object assignment•Object assignment does not copy values Person p1; Person p2; p1 = new Person("John"); p2 = p1; // p1 and p2 refer to the same person•Array assignment does not copy values int a1[ ]; int a2[ ]; a1 = new int[10]; a2 = a1; // a1 and a2 refer to the same arrayAn array’s size is not part of its type•When you declare an array, you declare its type; you must not specify its size–Example: String names[ ];•When you define the array, you allocate space; you must specify its size–Example: names = new String[50];•This is true even when the two are combined–Example: String names[ ] = new String[50];Array assignment•When you assign an array value to an array variable, the types must be compatible•The following is not legal: double dub[ ] = new int[10]; // illegal•The following is legal: int myArray[ ] = new int[10];–...and later in the program, myArray = new int[500]; // legal!–Legal because array size is not part of its typeExample of array use I•Suppose you want to find the largest value in an array scores of 10 integers: int largestScore = 0; for (int i = 0; i < 10; i++) if (scores[i] > largestScore) largestScore = scores[i];•By the way, do you see an error in the above program?–What if all values in the array are negative?Example of array use II•To find the largest value in an array scores of 10 (possibly negative) integers: int largestScore = scores[0]; for (int i = 1; i < 10; i++) if (scores[i] > largestScore) largestScore = scores[i];Example of array use III•Suppose you want to find the largest value in an array scores and the location in which you found it: int largestScore = scores[0]; int index = 0; for (int i = 1; i < 10; i++) if (scores[i] > largestScore) { largestScore = scores[i]; index = i;}Array names I•The rules for variables apply to arrays.•Rule 25: Use lowercase for the first word and capitalize only the first letter of each subsequent word that appears in a variable name.•Rule 26: Use nouns to name variables.•Rule 27: Pluralize the names of collection references.Array names II•Here’s what the naming rules mean:–Capitalization is just like any other variable–Array names should be plural nouns•Example array names: scores phoneNumbers preferredCustomersLength of an array•Arrays are objects•Every array has an instance constant, length, that tells how large the array is•Example: for (int i = 0; i < scores.length; i++) System.out.println(scores[i]);•Use of length is always preferred over using a constant such as 10•Strings have a length() method!Magic numbers•Use names instead of numbers in your code–Names help document the code; numbers don’t–It may be hard to tell why a particular number is used--we call it a magic number•This is a pejorative term–You might change your name about the value of a “constant” (say, more than ten scores)•You can change the value of a name in one place–An array’s length is always correct!NullPointerException•Suppose you declare a Person p but you don’t define it–Until you define p, it has the special value null–null is a legal value for any kind of object–null can be assigned, tested, and printed–But if you try to use a field or method of null, such as p.name or p.birthday(), the error you get is a nullPointerExceptionArrays of objects•Suppose you declare and define an


View Full Document

Penn CIT 591 - Arrays

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?