Download presentation
Presentation is loading. Please wait.
Published byCameron Winfred Johns Modified over 9 years ago
1
Java the UML Way http://www.tisip.no/JavaTheUmlWay/ versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9 Arrays of Primitive Data Types The array data structurepage 2-4 Copying an arraypage 5-7 The Month classpage 8 Sortingpage 9-12 Array as argumentpage 13 Searchingpage 14-15 The java.util.Arrays classpage 16 Two-dimensional arrayspage 17-18 More than two dimensionspage 19-21
2
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 2 Why Do We Need the Array Data Structure? A data structure is a collection of data that is stored in the internal memory under one name. An object is a data structure. An array is a data structure where all data belong to the same data type. Example: The precipitation data for every day in a month. How to declare the instance variables? private String monthName; private int precipitation1; private int precipitation2; private int precipitation3; private int precipitation4;..... private int precipitation31; Month monthName: String precipitation[28..31]: int getMaximum getNoOfDryDays getAverage
3
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 3 The Array Data Structure We declare an array: –int[] march = new int[31]; int[ ] march = new int[31]; 10 205 march 0010 [0] [1] [2] [3]............. [27] [28] [29] [30] index length
4
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 4 Using an Array Every element is accessed by indexing: –march[0] = 20; –march[15] = 30; –int sum = march[0] + march[1] + march[2]; The length of the array is given by the expression march.length. First element has index no. 0, last element no. (length-1). An invalid index throws an ArrayIndexOutOfBoundsException. We should check an index value before using it: –if (index >= 0 && index < march.length) number = march[index]; Summing up all precipitation in March: int sumMars = 0; for (int i = 0; i < march.length; i++) sumMars += march[i]; An array may be initialized in the declaration: –int[] oneWeek = {4, 5, 6, 7, 1, 2, 3}; // the length becomes 7 If we do not initialize the array, all elements are automatically initialized to 0. Solve all problems, page 244
5
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 5 Copying an Array What about using the assignment operator? This doesn't work…we get two references to the same array. int[] array1 = {1, 4, 6, -2}; int[] array2 = {7, 14, -6, 0}; 146-2 714-60 array1 array2 After the statement: array2 = array1; 146-2 714-60 array1 array2
6
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 6 An Array Has to be Copied Element by Element Solve problem 1 page 246. int[] array1 = {1, 4, 6, -2}; int[] array2 = {7, 14, -6, 0}; 146-2 714-60 array1 array2 714-60 714-60 array1 array2 Copying each element: for (int i = 0; i < array1.length; i++) { array1[i] = array2[i]; } Before copying: After copying:
7
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 7 System.arraycopy() may be Used for Copying Arrays int[] array1 = {1, 4, 6, -2}; int[] array2 = {7, 14, -6, 0}; 146-2 714-60 array1 array2 After the statement: System.arraycopy(array1, 1, array2, 2, 2); 146-2 71446 array1 array2 [0] [1] [2] [3]
8
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 8 The Month Class Show program listing 9.1 page 248-251. Solve problems 1 and 2, page 251. Month monthName: String precipitation[28..31]: int getMaximum getNoOfDryDays getAverage
9
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 9 Sorting Examples: –ranking the participants in a competition –sorting names in a telephone directory –ranking cities according to the price of local services. Distinguish between ascending sorts and descending sorts Assumptions: –There is room in the internal memory for the whole array which will be sorted –The data belong to a primitive data type—in other words, numbers or characters (the numeric characters ‘0’–‘9’ and the letters ‘a’–‘z’) A lot of different sorting algorithms. We'll look at ”Sorting by selection”.
10
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 10 Sorting by Selection for (start = 0; start < array.length; start++) { find the index for the smallest element in the interval [start, array.length - 1] replace smallest element with the element in the start position } 3 4 -5 13 10 0 8 -2 -5 4 3 13 10 0 8 -2 -5 -2 3 13 10 0 8 4 -5 -2 0 13 10 3 8 4 -5 -2 0 3 10 13 8 4 -5 -2 0 3 4 13 8 10 -5 -2 0 3 4 8 13 10 -5 -2 0 3 4 8 10 13 before we begin after first turn ready
11
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 11 The sortIntegerArray() Method – a Class Method package myLibrary; public class Sort { public static void sortIntegerArray(int[] array) { for (int start = 0; start < array.length; start++) { int smallestToNow = start; for (int i = start + 1; i < array.length; i++) { if (array[i] < array[smallestToNow]) smallestToNow = i; } int help = array[smallestToNow]; array[smallestToNow] = array[start]; array[start] = help; } /* This class consists of two more methods, see chapter 10. */ }
12
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 12 Using the sortIntegerArray() Method import myLibrary.*; class TestSort { public static void main(String[] args) { int[] test = {3, 4, -5, 13, 10, 0, 8, -2, 22, 15, 11, 9, 17}; Sort.sortIntegerArray(test); System.out.println("Sorted array: "); for (int i = 0; i < test.length; i++) System.out.print(test[i] + " "); System.out.println(); } If we want to sort decimal numerals, we have to write a sorting method where the parameter is of the double[] type and the help variable is of the double type.
13
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 13 Array as Argument The array name is a reference An array as argument means that we pass a reference to the method, and the method works with the same array elements as the client: Solve problem 3, page 254. 34-513 test client array sortIntegerArray Method invocation: Sort.sortIntegerArray(test); What’s happening behind the scenes: int[] array sortIntegerArray = test client........
14
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 14 Searching Searching in an array means finding the element or elements that satisfy specific criteria. For example, we indicate a value and go to find the index number for the element or elements that have this value. We also have to consider the possibility that this value may not be found at all. Sometimes it’s enough to find one occurrence of the value; at other times we have to find all occurrences. In program listing 9.1 there are two search examples: –getNoOfDryDays() –getDaysMax()
15
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 15 How to Program Searching? An example: Find the index for a day on which there was a specific amount of precipitation: public int getDay(int value) { for (int i = 0; i < precipitation.length; i++) { if (precipitation[i] == value) return i; // value found } return -1; // value not found } If we are not going to return the value: int dayNo = 0; while (dayNo < precipitation.length && precipitation[dayNo] != value) dayNo++; if (dayNo < precipitation.length) {...do what should be done if the value is found... } else {...do what should be done if the value is not found.. } Check the index before it is used! (we use the fact that Java uses short-circuit evaluation when evaulating boolean expressions) Solve problem 1, page 256.
16
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 16 The java.util.Arrays Class Offers class methods for sorting and searching Throws exceptions, see the online API-documentation In the method heads below, datatype is any primitive data type Sorting –static void sort(datatype[] array) –static void sort(datatype[] array, int fromIndex, int toIndex) Searching, the array is already sorted –static int binarySearch(datatype[] array, datatype searchValue) Filling the whole array or a part of it: –static void fill(datatype[] array, datatype value) –static void fill(datatype[] array, int fromIndex, int toIndex, datatype value) Comparing arrays (true is returned if they have the same length and the same contents) –static boolean equals(datatype[] array1, datatype[] array2) Solve problem 2 page 258.
17
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 17 A Need for a Two-Dimensional Array Sales data for every day for one year (52 weeks, 5 days per week) can be presented in the following manner: day 0day 1day 2day 3day 4 week 0100200150210300 week 1230200160300450 week 2120210180400 week 3 week 4 week 5...... week 51 Problem: Which services should an object that holds this information, offer its clients?
18
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 18 A Two-Dimensional Array A two-dimensional array has lines and columns Every line is itself a one-dimensional array with names as follows: sales[0], sales[1], sales[2], sales[3]. Examples: –sales[1][3] = 400; –int salget = sales[0][4]; –int sum = sales[3][0] + sales[3][1]; Show program listing 9.3, pp. 260-262. 100200150210 230200160300 120210180400 300 450 120 300310250240200 sales[0] sales[1] sales[2] sales[3] 0 1 2 3 4 sales[1][3] sales[3][4] int[ ][ ] sales = new int[4][5]; // 4 weeks, 5 days a week Solve problems 1 and 2, pp. 264-265.
19
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 19 More Than Two Dimensions Want sales data per salesperson: 200 450200 180200160300 210 180400 600 450 120 310 250240200 0 1 2 3 0 1 2 3 4 320450240220 140200160300 150210180400 760 450 120 110310250240200 100200150210 230200160300 120210180400 300 450 120 300310250240200 day no week no salesperson no 0 salesperson no 1 salesperson no 2 int[][][] sales = new int[3][4][5] noOfSalespersons noOfWeeks noOfDays sales[2][2][4] sales[0][0][0]
20
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 20 How to Handle the Three-Dimensional Array How much has the salesperson with the index salespersonNo sold in the week with index weekNo? int sum = 0; for (int i = 0; i < sales[salespersonNo][weekNo].length; i++) { sum += sales[salespersonNo][weekNo][i]; } How much has the salesperson with the index salespersonNo sold in total? int sum = 0; for (int weekNo = 0; weekNo < sales[salespersonNo].length; weekNo++) { for (int dayNo = 0; dayNo < sales[salespersonNo][weekNo].length; dayNo++) { sum += sales[salespersonNo][weekNo][dayNo]; } What is the total sales? int sum = 0; for (int salespersonNo = 0; salespersonNo < sales.length; salespersonNo++) { for (int weekNo = 0; weekNo < sales[salespersonNo].length; weekNo++) { for (int dayNo = 0; dayNo < sales[salespersonNo][weekNo].length; dayNo++) { sum += sales[salespersonNo][weekNo][dayNo]; }
21
Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 9, page 21 Multidimensional Arrays aren’t Used Very Much in Object-Oriented Programming Instead we make arrays of objects, where each object contains an array with additional information directly linked to the array. Multidimensional arrays are useful if we need to handle data in multiple dimensions.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.