COSC125_week3_Arrays.pptx
Document Details
Uploaded by Deleted User
Full Transcript
Data Structure and Programming Techniques (COSC 125) Introduction to Data Structures Arrays Ms Amal Nizar Data Structures Data Structures A data structure is a storage that is used to store and organize data. There are different basic and advanced types of data stru...
Data Structure and Programming Techniques (COSC 125) Introduction to Data Structures Arrays Ms Amal Nizar Data Structures Data Structures A data structure is a storage that is used to store and organize data. There are different basic and advanced types of data structures that are used in almost every program or software system that has been developed. Linear Data Structure Data elements are arranged sequentially or linearly, where each element is attached to its previous and next adjacent elements. Examples: Array, Stack, Queue, etc.. Static Data Structure: Static data structure has a fixed memory size. It is easier to access the elements in a static data structure. Example: array. Dynamic Data Structure: In dynamic data structure, the size is not fixed. It can be randomly updated during the runtime which may be considered efficient concerning the memory (space) complexity of the code. Example: Queue, Stack, etc. Non-Linear Data Structure Data structures where data elements are not placed sequentially or linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all the elements in a single run only. Examples: Trees and Graphs. Arrays Array is a collection of items of the same Arrays variable type that are stored at contiguous memory locations. Representing Arrays Array is a data structure that represents a collection of the same types of data. 9 Declaring Array Variables datatype[] arrayRefVar; Example: double[] myList; datatype arrayRefVar[]; // This style is allowed, but not preferred Example: double myList[]; 10 Creating Arrays arrayRefVar = new datatype[arraySize]; Example: myList = new double; myList references the first element in the array. myList references the last element in the array. 11 Declaring and Creating in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double; datatype arrayRefVar[] = new datatype[arraySize]; double myList[] = new double; 12 The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length For example, myList.length returns 10 13 Default Values When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, '\u0000' for char types, and false for boolean types. 14 Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList and myList to myList. myList = myList + myList; 15 Array Initializers Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement. 16 Declaring, creating, initializing Using the Shorthand Notation double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double; myList = 1.9; myList = 2.9; myList = 3.4; myList = 3.5; 17 CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] myList; myList = {1.9, 2.9, 3.4, 3.5}; 18 Processing Arrays See the examples in the text. 1. (Initializing arrays with input values) 2. (Initializing arrays with random values) 3. (Printing arrays) 4. (Summing all elements) 5. (Finding the largest element) 6. (Finding the smallest index of the largest element) 19 Initializing arrays with input values java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter " + myList.length + " values: "); for (int i = 0; i < myList.length; i++) myList[i] = input.nextDouble(); 20 Initializing arrays with random values for (int i = 0; i < myList.length; i++) { myList[i] = Math.random() * 100; } 21 Printing arrays for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " "); } 22 Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; } 23 Finding the largest element double max = myList; for (int i = 1; i < myList.length; i+ +) { if (myList[i] > max) max = myList[i]; } 24 Enhanced for Loop (for-each loop) JDK 1.5 introduced a new for loop that enables you to traverse the complete array sequentially without using an index variable. For example, the following code displays all elements in the array myList: for (double value: myList) System.out.println(value); In general, the syntax is for (elementType value: arrayRefVar) { // Process the value } You still have to use an index variable if you wish to traverse the array in a different order or change the elements in the array. 25 Analyze Numbers Read one hundred numbers, compute their average, and find out how many numbers are above the average. AnalyzeNumbers Run 26 Review Qn 1 Review Qn 2 Review Qn 3