1 Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.

Slides:



Advertisements
Similar presentations
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Introduction to Computers and Programming Lecture 17: Arrays (cont) Professor: Evan Korth New York University.
Chapter 7 Arrays.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Java vs. You.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 7 Arrays and Vectors Introducing Arrays Introducing Arrays Declaring Arrays, Creating Arrays, and Initializing Arrays Declaring Arrays, Creating.
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Searching Arrays Searching.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Arrays.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays F Introducing Arrays F Declaring Array Variables F Creating Arrays, and Initializing Arrays F Copying Arrays F Multidimensional Arrays.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Single-Dimensional Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
Single Dimensional Arrays
Chapter 7: Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Multidimensional Arrays
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 6 Arrays Solution Opening Problem
Chapter 8 Multidimensional Arrays
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Chapter 7 Multidimensional Arrays
Chapter 5 Arrays Introducing Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Single-Dimensional Arrays chapter6
Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 7 Multidimensional Arrays
Chapter 5 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Presentation transcript:

1 Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays F Multidimensional Arrays F Sorting and Search Methods

2 Introducing Arrays Array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects. An Array of 10 Elements of type double

3 Declaring Array Variables F datatype[] arrayname; Example: int[] myList; F datatype arrayname[]; Example: int myList[];

4 Creating Arrays arrayName = new datatype[arraySize]; Example: myList = new double[10];

5 Declaring and Creating in One Step F datatype[] arrayname = new datatype[arraySize]; double[] myList = new double[10]; F datatype arrayname[] = new datatype[arraySize]; double myList[] = new double[10];

6 Initializing Arrays F Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = (double)i; F Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5};

7 Example 5.1 Testing Arrays  Objective: The program receives 6 numbers from the keyboard, f inds the largest number and counts the occurrence of the largest number entered from the keyboard. Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its occurrence count is 4. TestArray Run

8 Example 5.2 Assigning Grades F Objective: read student scores (int) from the keyboard, get the best score, and then assign grades based on the following scheme: –Grade is A if score is >= best–10; –Grade is B if score is >= best–20; –Grade is C if score is >= best–30; –Grade is D if score is >= best–40; –Grade is F otherwise. AssignGrade Run

9 Passing Arrays to Methods Java uses pass by value to pass parameters to a method. There are important differences between passing a value of variables of primitive data types and passing arrays.  For a parameter of a primitive type value, the actual value is passed. Changing the value of the local parameter inside the method does not affect the value of the variable outside the method.  For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the method body will affect the original array that was passed as the argument.

10 Example 5.3 Passing Arrays as Arguments F Objective: Demonstrate differences of passing primitive data type variables and array variables. TestPassArrayRun

11 Example 5.3, cont.

12 Example 5.4 Computing Deviation Using Arrays DeviationRun

13 Copying Arrays

14 Copying Arrays Using a loop: int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i];

15 The arraycopy Utility arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

16 Multidimensional Arrays int[][] matrix = new int[10][10]; or int matrix[][] = new int[10][10]; for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { matrix[i][j] = (int)(Math.random()*1000); }

17 Example 5.6 Adding and Multiplying Two Matrices F Objective: Use two-dimensional arrays to create two matrices, and then add and multiple the two matrices. TestMatrixOperationRun

18 Ragged Arrays Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} };

19 Example 5.7 Using Arrays in Sorting  Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. SelectionSortRun

20 Example 5.8 Testing Linear Search  Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then displaying this array. Prompt the user to enter a key for testing the linear search. LinearSearchRun

21 Binary Search For binary search to work, the elements in the array must already be ordered. Without loss of generality, assume that the array is in ascending order. The binary search first compares the key with the element in the middle of the array. Consider the following three cases:

22 Binary Search, cont.   If the key is less than the middle element, you only need to search the key in the first half of the array.   If the key is equal to the middle element, the search ends with a match.   If the key is greater than the middle element, you only need to search the key in the second half of the array.

23 Example 5.9 Testing Binary Search  Objective: Implement and test the binary search method. The program first creates an array of 10 elements of int type. It displays this array and then prompts the user to enter a key for testing binary search. BinarySearchRun