Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.

Similar presentations


Presentation on theme: "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."— Presentation transcript:

1 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

2 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The Collections Framework: ArrayLists

3 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition3 Objectives (continued) Two-Dimensional Arrays Common Programming Errors

4 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition4 One-Dimensional Arrays List of related values with same data type –Stored using single group name Array declaration example: double prices[]; prices = new double[6];

5 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition5 One-Dimensional Arrays (continued) Using new operator: –Array elements automatically initialized to: 0 for numerical built-in types false for boolean built-in types null for reference types

6 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition6 One-Dimensional Arrays (continued) Figure 8.4: The results of the allocation prices = new double[6];

7 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition7 One-Dimensional Arrays (continued) Common programming practice: –Define number of array items as symbolic constant Element –Item in array Index –Position of item in array –Also called subscript

8 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition8 One-Dimensional Arrays (continued) grade[0] –Refers to first value stored in grade array –Read as “grade sub zero” –Used anywhere scalar variables valid Subscript contained within brackets not always integer constant –Can be any expression that evaluates to integer value

9 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition9 One-Dimensional Arrays (continued) Using integer expressions as subscripts allows sequencing through array using loop Example of looping through array: sum = 0; // initialize the sum to zero for (i = 0; i < NUMELS; i++) sum = sum + grade[i]; // add in a grade –i is used both as counter in for loop and as subscript

10 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition10 One-Dimensional Arrays (continued) When accessing array element: –Java does check value of index being used at run time –If index exceeds length of array Java will notify you of ArrayIndexOutOfBoundsException

11 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition11 One-Dimensional Arrays (continued) Figure 8.6: Identifying individual array elements

12 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition12 One-Dimensional Arrays (continued) Size of array –Automatically stored in variable named length Looping using for loop and array length: sum = 0; // initialize the sum to zero for (i = 0; i < grade.length; i++) sum = sum + grade[i]; // add in a grade

13 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition13 Input and Output of Array Values Input: –Individual array elements can be assigned values interactively using: readLine() showInputDialog() Output: –Array elements can be printed using: print() println()

14 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition14 String Arrays Arrays of reference data types may be constructed Declaring String array: –String names[] = new String[4]; Arrays of reference types stored differently from arrays of built-in data types

15 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition15 String Arrays (continued) Figure 8.8a: The declaration String names[], creates a single reference variable

16 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition16 String Arrays (continued) Figure 8.8b: The allocation names = new String[4] creates an array of references

17 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition17 String Arrays (continued) Figure 8.8c: The assignment of values creates actual array objects

18 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition18 Run-Time Dimensioning Size of array can also be entered interactively at run time Entered value can be used to allocate space for array –Using new operator

19 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition19 Array Initialization Arrays can be initialized within declaration statements –May continue across multiple lines –No method of indicating repetition of initialization value –No way to initialize later array elements without first specifying values for earlier elements Example: –int grade[] = {98, 87, 92, 79, 85};

20 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition20 Deep and Shallow Copies Deep copy –Element-by-element copy Shallow copy –Produced when array assignment is executed

21 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition21 Deep and Shallow Copies (continued) System.arraycopy() method –Allocate and initializes array –Copies user-specified number of elements from one array to second array –Syntax: System.arraycopy(source array name, starting source element index, target array name, starting target element index, number of elements to be copied); –Provides deep copy

22 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition22 Deep and Shallow Copies (continued) Figure 8.12: The result of a shallow copy

23 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition23 The Arrays Class: Searching and Sorting Arrays class –Do not confuse with Array class –Helper class –Provides number of extremely useful methods Great help in processing arrays –Contains methods for: Sorting array Searching sorted array for particular item

24 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition24 The sort() and binarySearch() Methods sort() method –Arranges elements of array into ascending (increasing) order –Uses modified quicksort algorithm –Should be called before binarySearch()

25 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition25 The sort() and binarySearch() Methods (continued) binarySearch() method –Searches array for specified value –Requires sorted array –Returns: Item index if found Negative number if not found

26 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition26 Arrays as Arguments Individual array elements passed to called method –In same manner as individual scalar variables –Passed by value Complete array passed to called method –Passed by reference –Called method may change items in original array

27 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition27 Arrays as Arguments (continued) Figure 8.15: The location of the array is passed

28 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition28 The Collections Framework: ArrayLists Array –Data structure of choice for fixed-length collections of data that are related Many programming applications require variable- length lists –Java provides set of classes referred to as collections framework –Provides seven different types of generic data structures

29 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition29 The Collections Framework: ArrayLists (continued) Figure 8.16: The collections framework container types

30 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition30 The Collections Framework: ArrayLists (continued) Collections class –Supports container classes –Provides functions: Searching Sorting Random shuffling Reverse-ordering

31 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition31 The Collections Framework: ArrayLists (continued) Other classes: –Iterator –Comparator –Comparable Framework containers –Most useful for lists that must be expanded and contracted –Cannot store primitive data types directly

32 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition32 The Collections Framework: ArrayLists (continued) Array container –Useful for fixed-length lists

33 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition33 The Collections Framework: ArrayLists (continued) Table 8.1: Collections Framework Classes

34 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition34 The ArrayList Class Stores elements that can be accessed using integer index Automatically expand as needed Can be easily contracted Components: –Reference variable –Actual ArrayList –Array elements

35 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition35 The ArrayList Class (continued) Syntax: –ArrayList x = new ArrayList (); can be –Primitive type –Reference type Generic Types feature –Allows objects in ArrayList to be referenced as instances of particular type without casting

36 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition36 The ArrayList Class (continued) Figure 8.17: An ArrayList structure

37 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition37 The Collections Helper Class Table 8.3: Summary of Commonly Used Collections Class Methods

38 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition38 Autoboxing and Auto-unboxing New feature of Java Primitive types automatically boxed with appropriate wrapper class when: –Added to container –Initializing variable of wrapper class type with primitive value –Assigning primitive value to wrapper class variable Primitive types auto-unboxed as well

39 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition39 The Iterator Class Similar to array’s index Generalized index –Keeps track of object’s position within container For some classes provides primary means of accessing individual elements Obtaining iterator: –Iterator iter = x.iterator();

40 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition40 The Iterator Class (continued) Table 8.4: The Iterator Class Methods

41 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition41 Enhanced for Loop for Iteration for loop syntax: for (FormalParameter : Expression) Statement Simplified for loop does not require iterator Expression must be array or instance of java.lang.Iteratable

42 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition42 Parallel Arrays as Records Parallel arrays –Corresponding data in record resides in same position in more than one array –Required in earlier programming languages that only supported array data structures –In Java programmer should: Choose to combine parallel elements in object Store objects in one-dimensional array

43 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition43 Two-Dimensional Arrays Consists of both rows and columns of elements Sometimes called a table Example declaration: –int val[][]; Example allocation: –val = new int[3][4]; Elements are identified by position in array

44 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition44 Two-Dimensional Arrays (continued) Can be initialized from within declaration statements: –int val[][] = {{8,16,9,52}, {3,15,27,6}, {7,25,2,10}}; Number of columns need not be same for each row Processed using nested for loops

45 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition45 Two-Dimensional Arrays (continued) Figure 8.22: Each array element is identified by its row and column position

46 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition46 Two-Dimensional Arrays (continued) val.length –Provides number of rows in array referenced by val val[i].length –Provides number of columns in i th row of val array

47 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition47 Two-Dimensional Arrays (continued) Passing Two-Dimensional Arrays –Identical to passing one-dimensional array –Called method receives access to entire array

48 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition48 Advanced Dimensioning Capabilities Java provides capability to create two-dimensional arrays where each row has different number of columns To create: –Use an initializing list that explicitly lists values for each row –Use new operator and place values into newly created array

49 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition49 Larger Dimensional Arrays Java allows any number of array dimensions to be created Similar to creating and allocating two-dimensional arrays Declaration: int val[][][]; val = new int[3][2][2];

50 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition50 Larger Dimensional Arrays (continued) Figure 8.23: The representation of a three- dimensional array

51 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition51 Common Programming Errors Forgetting empty bracket pairs when declaring an array’s name Declaring array reference variable using explicit dimension sizes Using subscript that references nonexistent array element Not using large enough counter value in for loop counter to cycle through all array elements

52 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition52 Summary One-dimensional array –Data structure –Stores list of values of same data type Array elements –Stored in contiguous locations in memory –Referenced using array name and subscript Such as num[22]

53 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition53 Summary (continued) Two-dimensional array declared by providing: –Data type –Reference variable name –Two sets of empty bracket pairs after the array’s name Arrays may be initialized when they are declared Collections framework –Set of classes providing generic data structures


Download ppt "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."

Similar presentations


Ads by Google