140 likes | 223 Views
Lecture 5 of Computer Science II. Arrays. Instructor: Mr.Ahmed Al Astal. 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.
E N D
Lecture 5 ofComputer Science II Arrays Instructor: Mr.Ahmed Al Astal
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. Page 2
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
Using Arrays Insertion • 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
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. 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
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 Page 6
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. Page 7
Using Arrays A Simple Insertion-Sort Algorithm High-level description of the insertion-sort algorithm. Page 8
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.
Using Arrays A Simple Insertion-Sort Algorithm Intermediate-level description of the insertion-sort algorithm. Page 10
Using Arrays A Simple Insertion-Sort Algorithm Execution of the insertion-sort algorithm on an array of eight characters. Page 11
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 12
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. Page 13
Using Arrays Tic-Tac-Toe Game 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 14