Java the UML Way versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and.

Slides:



Advertisements
Similar presentations
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.
Arrays Chapter 6 Chapter 6.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
An Introduction to Programming with C++ Fifth Edition
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Class design. int month; int year class Month Defining Classes A class contains data declarations (state) and method declarations (behaviors) Data declarations.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 9 Introduction to Arrays
Introduction to Arrays. Useful Array Operations  Finding the Highest Value int [] numbers = new int[50]; int highest = numbers[0]; for (int i = 1; i.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Text Book: M. T. Goodrich and R. Tamassia, "Data Structures and Algorithms in Java," 4th edition, 2006, Wiley & Sons, Inc., ISBN Text Book:
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Java Programming: From Problem Analysis to Program Design, 4e
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays.
Presentation transcript:

Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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 The java.util.Arrays classpage 16 Two-dimensional arrayspage More than two dimensionspage 19-21

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 9, page 3 The Array Data Structure We declare an array: –int[] march = new int[31]; int[ ] march = new int[31]; march 0010 [0] [1] [2] [3] [27] [28] [29] [30] index length

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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}; array1 array2 After the statement: array2 = array1; array1 array2

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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}; array1 array array1 array2 Copying each element: for (int i = 0; i < array1.length; i++) { array1[i] = array2[i]; } Before copying: After copying:

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 9, page 7 System.arraycopy() may be Used for Copying Arrays int[] array1 = {1, 4, 6, -2}; int[] array2 = {7, 14, -6, 0}; array1 array2 After the statement: System.arraycopy(array1, 1, array2, 2, 2); array1 array2 [0] [1] [2] [3]

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 9, page 8 The Month Class Show program listing 9.1 page Solve problems 1 and 2, page 251. Month monthName: String precipitation[28..31]: int getMaximum getNoOfDryDays getAverage

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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”.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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 } before we begin after first turn ready

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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. */ }

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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 test client array sortIntegerArray Method invocation: Sort.sortIntegerArray(test); What’s happening behind the scenes: int[] array sortIntegerArray = test client

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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()

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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 week week week 3 week 4 week week 51 Problem: Which services should an object that holds this information, offer its clients?

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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 sales[0] sales[1] sales[2] sales[3] sales[1][3] sales[3][4] int[ ][ ] sales = new int[4][5]; // 4 weeks, 5 days a week Solve problems 1 and 2, pp

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 9, page 19 More Than Two Dimensions Want sales data per salesperson: 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]

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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]; }

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, 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.