DOC PREVIEW
UNF COP 2551 - Study Notes

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 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 33 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 33 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 33 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 33 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 33 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 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 7ArraysOutlineSlide 4Slide 5Slide 6Slide 7Slide 8Declaring ArraysSlide 10Using ArraysSlide 12Bounds CheckingSlide 14Slide 15Slide 16Alternate Array SyntaxInitializer ListsSlide 19Arrays as Parameters: Call by ReferenceArrays as Parameters: Call by ValueSlide 22Arrays of ObjectsSlide 24Slide 25Slide 26Slide 27Arrays of Objects – Discuss the UMLSlide 29Slide 30Slide 31Slide 32Command-Line ArgumentsChapter 7Arrays: Part 1 of 2© 2004 Pearson Addison-Wesley. All rights reserved 7-2Arrays•Arrays are objects that help us organize large amounts of information•Chapter 7 focuses on:array declaration and usebounds checking and capacityarrays that store object referencesvariable length parameter listsmultidimensional arraysthe ArrayList class© 2004 Pearson Addison-Wesley. All rights reserved 7-3OutlineDeclaring and Using ArraysArrays of ObjectsVariable Length Parameter ListsTwo-Dimensional ArraysThe ArrayList Class© 2004 Pearson Addison-Wesley. All rights reserved 7-4Arrays•An array is an ordered list of values0 1 2 3 4 5 6 7 8 979 87 94 82 67 98 87 81 74 91An array of size N is indexed from zero to N-1scoresThe entire arrayhas a single nameEach value has a numeric ind e xThis array holds 10 values that are indexed from 0 to 9© 2004 Pearson Addison-Wesley. All rights reserved 7-5Arrays•A particular value in an array is referenced using the array name followed by the index in brackets•For example, the expressionscores[2] refers to the value 94 (the 3rd value in the array)•That expression represents a place to store a single integer and can be used wherever an integer variable can be used0 1 2 3 4 5 6 7 8 979 87 94 82 67 98 87 81 74 91scores© 2004 Pearson Addison-Wesley. All rights reserved 7-6Arrays•Array element - assigned a value, printed, or used in a calculation just like any other variable,•Array must be unambiguously usedscores[2] = 89;a = scores[10];scores[first] = scores[first] + 2;scores[x] = scores[4} * 2; //assumes x is intmean = (scores[0] + scores[1])/2;System.out.println ("Top = " + scores[5]); Need both array name and index together to reference a specific element in the array!© 2004 Pearson Addison-Wesley. All rights reserved 7-7Arrays•The values held in an array are called array elements•An array stores multiple values of the same type – the element type (like all integers, or characters, …)The element type can be a primitive type or an object reference Therefore, we can create an array of integers, an array of characters, an array of String objects, an array of Coin objects, array of Country objects… In Java, the array itself is an object; must be instantiated (will show ahead)© 2004 Pearson Addison-Wesley. All rights reserved 7-8Arrays•Another way to depict the scores array:scores79879482679887817491This is telling you that scores (itself) is a Reference!‘Scores’ points to the array.© 2004 Pearson Addison-Wesley. All rights reserved 7-9Declaring Arrays•The scores array could be declared as follows:int[] scores = new int[10];•Note the syntax, the ‘reference’ and the new object! It also says that there will be 10 scores referenced.•The type of the variable scores is int[] (“an array of integers” or “an array of ints.”) Note that the array type does not specify its size, but each object of that type has a specific size•The reference variable scores is set to a new array object that can hold 10 integers© 2004 Pearson Addison-Wesley. All rights reserved 7-10Declaring Arrays•Some other examples of array declarations: float[] prices = new float[500];boolean[] flags; flags = new boolean[20];char[] codes = new char[1750];Can you provide examples of each type? Can you draw such an array as was provided earlier in these slides?You saw the entries for the integer array, scores.What would a similar array of flags look like. Draw it.Of characters? Draw it. (You will see this again!)© 2004 Pearson Addison-Wesley. All rights reserved 7-11Using Arrays•The iterator version of the for loop can be used when processing array elementsfor (int score : scores) System.out.println (score);•This is only appropriate when processing all array elements from top (lowest index) to bottom (highest index)•BasicArray.java (page 372)  (array name)© 2004 Pearson Addison-Wesley. All rights reserved 7-12public class BasicArray{ //----------------------------------------------------------------- // Creates an array, fills it with various integer values, // modifies one value, then prints them out. //----------------------------------------------------------------- public static void main (String[] args) { final int LIMIT = 15, MULTIPLE = 10; int [ ] list = new int [LIMIT]; // creates an array object list of 15 ints // Initialize the array values for (int index = 0; index < LIMIT; index++) list[index] = index * MULTIPLE; // what does this do? list[5] = 999; // change one array value // Print the array values for (int value : list) // Note: uses iterator version of the ‘for’ stmt. System.out.print (value + " "); // list indexed by value will print. } // end main} // end BasicArray© 2004 Pearson Addison-Wesley. All rights reserved 7-13Bounds Checking•Once an array is created, it has a fixed size•An index used in an array reference must specify a valid elementThat is, the index value must be in range 0 to n-1•The Java interpreter throws an ArrayIndexOutOfBoundsException if an array index is out of bounds •This is called automatic bounds checking© 2004 Pearson Addison-Wesley. All rights reserved 7-14Bounds Checking•For example, if the array codes can hold 100 values, it can be indexed using only the numbers 0 to 99•If the value of count is 100, then the following reference will cause an exception to be thrown:System.out.println (codes[count]);•It’s common to introduce off-by-one errors when using arraysfor (int index=0; index <= 100; index++)codes[index] = index*50 + epsilon;problem© 2004 Pearson Addison-Wesley. All rights reserved 7-15Bounds Checking•Each array object has a public constant called length that stores the size of the arrayIt is referenced using the array name:scores.length NOT scores.length()•Note that length holds the number of elements, not the largest index•See


View Full Document

UNF COP 2551 - Study Notes

Download Study Notes
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 Study Notes 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 Study Notes 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?