Your SlideShare is downloading. ×
0
Chapter 5 Arrays <ul><li>Introducing Arrays </li></ul><ul><li>Declaring Array Variables, Creating Arrays, and Initializing...
Introducing Arrays Array is a data structure that represents a collection of the same types of data. Java treats these arr...
Declaring Array Variables <ul><li>datatype[] arrayname; </li></ul><ul><li>Example:  </li></ul><ul><li>int[] myList; </li><...
Creating Arrays <ul><li>arrayName = new datatype[arraySize]; </li></ul><ul><li>Example: </li></ul><ul><li>myList = new dou...
Declaring and Creating in One Step <ul><li>datatype[] arrayname = new </li></ul><ul><li>datatype[arraySize]; </li></ul><ul...
Initializing Arrays <ul><li>Using a loop: </li></ul><ul><li>for (int i = 0; i < myList.length; i++) </li></ul><ul><li>myLi...
Example 5.1 Testing Arrays <ul><li>Objective: The program receives 6 numbers from the keyboard, f inds the largest number ...
Example 5.2 Assigning Grades <ul><li>Objective: read student scores (int) from the keyboard, get the best score, and then ...
Passing Arrays to Methods <ul><li>Java uses  pass by value  to pass parameters to a method. There are important difference...
foreach Loops <ul><li>int[] sourceArray = {2, 3, 1, 5, 10}; </li></ul><ul><li>foreach (int element: sourceArray) </li></ul...
Example 5.3 Passing Arrays as Arguments <ul><li>Objective: Demonstrate differences of passing primitive data type variable...
Example 5.3, cont.
Example 5.4 Computing Deviation Using Arrays Deviation Run
Copying Arrays
Copying Arrays <ul><li>Using a loop: </li></ul><ul><li>int[] sourceArray = {2, 3, 1, 5, 10}; </li></ul><ul><li>int[] targe...
The  arraycopy  Utility <ul><li>Copy the content from one array to another array. </li></ul><ul><li>arraycopy(sourceArray,...
Multidimensional Arrays <ul><li>int[][] matrix = new int[10][10]; </li></ul><ul><li>or </li></ul><ul><li>int matrix[][] = ...
Example 5.6 Adding and Multiplying Two Matrices <ul><li>Objective: Use two-dimensional arrays to create two matrices, and ...
Ragged Arrays <ul><li>Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Suc...
Ragged Arrays <ul><li>int [][] rArray = new iint [5][]; </li></ul><ul><li>rArray[0] = new int[5]; </li></ul><ul><li>rArray...
Example 5.7 Using Arrays in Sorting <ul><li>Objective: Use the  selectionSort  method to write a program that will sort a ...
Example 5.8 Testing Linear Search <ul><li>Objective: Implement and test the linear search method by creating an array of 1...
Binary Search <ul><li>For binary search to work, the elements in the array must already be ordered. Without loss of genera...
Binary Search, cont. <ul><li>    If the key is less than the middle element, you only need to search the key in the first...
Example 5.9 Testing Binary Search <ul><li>Objective: Implement  and test the binary search method. The program first creat...
Upcoming SlideShare
Loading in...5
×

05slide

1,071

Published on

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,071
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
66
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "05slide"

  1. 1. Chapter 5 Arrays <ul><li>Introducing Arrays </li></ul><ul><li>Declaring Array Variables, Creating Arrays, and Initializing Arrays </li></ul><ul><li>Passing Arrays to Methods </li></ul><ul><li>Copying Arrays </li></ul><ul><li>Multidimensional Arrays </li></ul><ul><li>Sorting and Search Methods </li></ul>
  2. 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. 3. Declaring Array Variables <ul><li>datatype[] arrayname; </li></ul><ul><li>Example: </li></ul><ul><li>int[] myList; </li></ul><ul><li>datatype arrayname[]; </li></ul><ul><li>Example: </li></ul><ul><li>int myList[]; </li></ul>
  4. 4. Creating Arrays <ul><li>arrayName = new datatype[arraySize]; </li></ul><ul><li>Example: </li></ul><ul><li>myList = new double[10]; </li></ul>
  5. 5. Declaring and Creating in One Step <ul><li>datatype[] arrayname = new </li></ul><ul><li>datatype[arraySize]; </li></ul><ul><li>double[] myList = new double[10]; </li></ul><ul><li>datatype arrayname[] = new datatype[arraySize]; </li></ul><ul><li>double myList[] = new double[10]; </li></ul>
  6. 6. Initializing Arrays <ul><li>Using a loop: </li></ul><ul><li>for (int i = 0; i < myList.length; i++) </li></ul><ul><li>myList[i] = (double)i; </li></ul><ul><li>Declaring, creating, initializing in one step: </li></ul><ul><li>double[] myList = {1.9, 2.9, 3.4, 3.5}; </li></ul>
  7. 7. Example 5.1 Testing Arrays <ul><li>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. </li></ul><ul><li>Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its occurrence count is 4. </li></ul>TestArray Run
  8. 8. Example 5.2 Assigning Grades <ul><li>Objective: read student scores (int) from the keyboard, get the best score, and then assign grades based on the following scheme: </li></ul><ul><ul><li>Grade is A if score is >= best–10; </li></ul></ul><ul><ul><li>Grade is B if score is >= best–20; </li></ul></ul><ul><ul><li>Grade is C if score is >= best–30; </li></ul></ul><ul><ul><li>Grade is D if score is >= best–40; </li></ul></ul><ul><ul><li>Grade is F otherwise. </li></ul></ul>AssignGrade Run
  9. 9. Passing Arrays to Methods <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
  10. 10. foreach Loops <ul><li>int[] sourceArray = {2, 3, 1, 5, 10}; </li></ul><ul><li>foreach (int element: sourceArray) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(element); } </li></ul><ul><li>to traverse each element in the array </li></ul><ul><li>Cannot change element in the array </li></ul><ul><li>The variable element must be declared the same type as the elements in sourceArray. </li></ul>
  11. 11. Example 5.3 Passing Arrays as Arguments <ul><li>Objective: Demonstrate differences of passing primitive data type variables and array variables. </li></ul>TestPassArray Run
  12. 12. Example 5.3, cont.
  13. 13. Example 5.4 Computing Deviation Using Arrays Deviation Run
  14. 14. Copying Arrays
  15. 15. Copying Arrays <ul><li>Using a loop: </li></ul><ul><li>int[] sourceArray = {2, 3, 1, 5, 10}; </li></ul><ul><li>int[] targetArray = new int[sourceArray.length]; </li></ul><ul><li>for (int i = 0; i < sourceArrays.length; i++) </li></ul><ul><li>targetArray[i] = sourceArray[i]; </li></ul>
  16. 16. The arraycopy Utility <ul><li>Copy the content from one array to another array. </li></ul><ul><li>arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); </li></ul><ul><li>Example: </li></ul><ul><li>System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); </li></ul>
  17. 17. Multidimensional Arrays <ul><li>int[][] matrix = new int[10][10]; </li></ul><ul><li>or </li></ul><ul><li>int matrix[][] = new int[10][10]; </li></ul><ul><li>for (int i=0; i<matrix.length; i++) </li></ul><ul><li>for (int j=0; j<matrix[i].length; j++) </li></ul><ul><li>{ </li></ul><ul><li>matrix[i][j] = (int)(Math.random()*1000); </li></ul><ul><li>} </li></ul><ul><li>For 3 dimensional array, refer to text book, pg 225 to 227. </li></ul>
  18. 18. Example 5.6 Adding and Multiplying Two Matrices <ul><li>Objective: Use two-dimensional arrays to create two matrices, and then add and multiple the two matrices. </li></ul>TestMatrixOperation Run
  19. 19. Ragged Arrays <ul><li>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, </li></ul><ul><li>int[][] matrix = </li></ul><ul><li>{ </li></ul><ul><li>{1, 2, 3, 4, 5}, </li></ul><ul><li>{2, 3, 4, 5}, </li></ul><ul><li>{3, 4, 5}, </li></ul><ul><li>{4, 5}, </li></ul><ul><li>{5} </li></ul><ul><li>}; </li></ul>
  20. 20. Ragged Arrays <ul><li>int [][] rArray = new iint [5][]; </li></ul><ul><li>rArray[0] = new int[5]; </li></ul><ul><li>rArray[1] = new int[4]; </li></ul><ul><li>rArray[2] = new int[3]; </li></ul><ul><li>rArray[3] = new int[2]; </li></ul><ul><li>rArray[4] = new int[1]; </li></ul><ul><li>//the size of the 1 st dimension of the array must be specified. </li></ul>
  21. 21. Example 5.7 Using Arrays in Sorting <ul><li>Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. </li></ul>SelectionSort Run
  22. 22. Example 5.8 Testing Linear Search <ul><li>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. </li></ul>LinearSearch Run
  23. 23. Binary Search <ul><li>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. </li></ul><ul><li>The binary search first compares the key with the element in the middle of the array. Consider the following three cases: </li></ul>
  24. 24. Binary Search, cont. <ul><li>    If the key is less than the middle element, you only need to search the key in the first half of the array. </li></ul><ul><li>    If the key is equal to the middle element, the search ends with a match. </li></ul><ul><li>    If the key is greater than the middle element, you only need to search the key in the second half of the array. </li></ul>
  25. 25. Example 5.9 Testing Binary Search <ul><li>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. </li></ul>BinarySearch Run
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×