Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 of Computer Science II

Similar presentations


Presentation on theme: "Lecture 5 of Computer Science II"— Presentation transcript:

1 Lecture 5 of Computer Science II
Arrays

2 Storing Game Entries in an Array
Using Arrays Storing Game Entries in an Array The first application we study is for storing entries in an array—in particular, high score entries for a video game. we have decided to store high score entries, which is a simple application that presents some important data structuring concepts that we will use for other implementations in this book. what we want to include in a high score entry? an integer representing the score itself, which we will call score. name of the person earning this score, which we will simply call name. Java code for a simple GameEntry class. Note that we include methods for returning the name and score for a game entry object, as well as a method for return a string representation of this entry.  Page 2

3 Storing Game Entries in an Array
Using Arrays Storing Game Entries in an Array An illustration of an array of length ten storing references to six GameEntry objects in the cells from index 0 to 5, with the rest being null references.  Page 3

4 Insertion Using Arrays
One of the most common updates we might want to make to the entries array of high scores is to add a new game entry. add(e): Insert game entry e into the collection of high scores. If the collection is full, then e is added only if its score is higher than the lowest score in the set, and in this case, e replaces the entry with the lowest score. Preparing to add a new GameEntry object to the entries array. In order to make room for the new reference, we have to shift the references to game entries with smaller scores than the new one to the right by one cell.  Page 4

5 Java code for inserting a GameEntry object.
Using Arrays Insertion Once we have identified the place in the entries array where the new game entry, e, belongs, we add a reference to e at this position. Java code for inserting a GameEntry object. Adding a reference to a new GameEntry object to the entries array. The reference can now be inserted at index 2, since we have shifted all references to GameEntry objects with scores less than the new one to the right.  Page 5

6 Java code for performing the remove operation.
Using Arrays Object Removal let us consider how we might remove a reference to a GameEntry object from the entries array. remove(i): Remove and return the game entry e at index i in the entries array. all objects previously stored at indices higher than i are "moved over" to fill in for the removed object Java code for performing the remove operation.  Page 6

7 Java code for performing the remove operation.
Using Arrays Sorting an Array In this section, we study a way of starting with an array with objects that are out of order and putting them in order. A Simple Insertion-Sort Algorithm Insertion sort is the algorithm many people use when sorting a hand of cards. Java code for performing the remove operation.  Page 7

8 High-level description of the insertion-sort algorithm.
Using Arrays A Simple Insertion-Sort Algorithm High-level description of the insertion-sort algorithm. This simple insertion–sort algorithm goes as follows. We start with the first character in the array. One character by itself is already sorted. Then we consider the next character in the array. If it is smaller than the first, we swap them. Next we consider the third character in the array. We swap it leftward until it is in its proper order with the first two characters. We continue in this manner with the fourth, fifth integer, the sixth, and so on, until the whole array is sorted.  Page 8

9 A Simple Insertion-Sort Algorithm
Using Arrays A Simple Insertion-Sort Algorithm Intermediate-level description of the insertion-sort algorithm. Java code for performing insertion-sort on an array of characters.  Page 9

10 A Simple Insertion-Sort Algorithm
Using Arrays A Simple Insertion-Sort Algorithm Execution of the insertion-sort algorithm on an array of eight characters. An interesting thing happens in the insertion-sort algorithm if the array is already sorted. In this case, the inner loop does only one comparison, determines that there is no swap needed, and returns back to the outer loop. That is, we perform only one iteration of the inner loop for each iteration of the outer loop. Thus, in this case, we perform a minimum number of comparisons. Of course, we might have to do a lot more work than this if the input array is extremely out of order. In fact, we will have to do the most work if the input array is in decreasing order.  Page 10

11 java.util Methods for Arrays and Random Numbers
Using Arrays java.util Methods for Arrays and Random Numbers Java provides a number of built-in methods for performing common tasks on arrays, they are associated with the class, java.util.Arrays : equals(A, B): Returns true if and only if the array A and the array B are equal. fill(A,x): Stores element x into every cell of array A. sort(A): Sorts the array A using the natural ordering of its elements. toString(A): Returns a String representation of the array A. For example, the following string would be returned by the method toString called on an array of integers A = [4,5,2,3,5,7,10]: [4, 5, 2, 3, 5, 7, 10]  Page 11

12 Two-Dimensional Arrays and Positional Games
Using Arrays Two-Dimensional Arrays and Positional Games Arrays in Java are one-dimensional. there is a way we can define two-dimensional arrays in Java—we can create a two-dimensional array as an array of arrays. Such a two—dimensional array is sometimes also called a matrix. In Java, we declare a two—dimensional array as follows: int[ ][ ] Y = new int[8][10]; Two-dimensional arrays have many applications to numerical analysis. we explore an application of two- dimensional arrays for implementing a simple positional game. Illustration of a two-dimensional integer array, Y, which has 8 rows and 10 columns. The value of Y[3][5] is 100 and the value of Y[6][2] is 632.  Page 12

13 Tic-Tac-Toe Game Using Arrays
The basic idea is to use a two-dimensional array, board, to maintain the game board. Cells in this array store values that indicate if that cell is empty or stores an X or O. A simple, complete Java class for playing Tic-Tac-Toe between two players is provides at page No. 134 ( The text Book)  Page 13


Download ppt "Lecture 5 of Computer Science II"

Similar presentations


Ads by Google