COSC125_week3_Arrays_Session 2.pptx

Full Transcript

Data Structure and Programming Techniques (COSC 125) Single Dimensional Arrays Multidimensional Arrays Ms Amal Nizar Single Dimensional Arrays Single Dimensional Arrays A one-dimensional array in Java is a collection of similar types of elements stored at contiguous memory locations....

Data Structure and Programming Techniques (COSC 125) Single Dimensional Arrays Multidimensional Arrays Ms Amal Nizar Single Dimensional Arrays Single Dimensional Arrays A one-dimensional array in Java is a collection of similar types of elements stored at contiguous memory locations. The data is stored in a continuous manner, which makes operations like search, delete, insert etc., much easier. Recap Declaring Array Variables Creating Arrays Array size & Default values Accessing array elements. Array initializers Processing Arrays – Examples using for loops Multi Dimensional Arrays Two-Dimensional Array Declaring & Creating Length of 2-dimensional array Length of 2-dimensional array Processing 2 dimensional array Processing 2-dimensional array Processing 2-dimensional array Processing 2-dimensional array Processing 2-dimensional array Programming Exercise Write a program that find the row with largest sum in a 2-dimensional array. Tips: Use variables maxRow and indexOfMaxRow to track the largest sum and index of the row. For each row, compute its sum and update maxRow and indexOfMaxRow if the new sum is greater. Solution: Copying Arrays – Method 1 To copy the contents of one array into another, you must copy the array’s individual elements into the other array, 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]; 21 The arraycopy Utility – Method 2 arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); The parameters srcPos and tarPos indicate the starting positions in sourceArray and targetArray, respectively. The number of elements copied from sourceArray to targetArray is indicated by length. 22 Practice Qn: Passing Arrays to Methods When passing an array to a method, the reference of the array is passed to the method. Just as you can pass primitive type values to methods, you can also pass arrays to methods. Passing Arrays to Methods For example, the following method displays the elements in an int array: 25 Anonymous Array The statement printArray(new int[]{3, 1, 2, 6, 4, 2}); creates an array using the following syntax: new dataType[]{literal0, literal1,..., literalk}; There is no explicit reference variable for the array. Such array is called an anonymous array. 26 Pass By Value Java uses pass by value to pass arguments 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. 27 What is the output ? Answer Explanation Returning an array from a method When a method returns an array, the reference of the array is returned. Passing 2 dimensional arrays to methods When passing a two-dimensional array to a method, the reference of the array is passed to the method. You can pass a two-dimensional array to a method just as you pass a one-dimensional array. You can also return an array from a method. Programming Exercise 2: Write a program that reads a 2-dimensional integer array and calculates sum of its elements using 2 methods. The first method, getArray(), returns a two- dimensional array and the second method, sum(int[][] m), returns the sum of all the elements in a matrix. Sample Output:

Use Quizgecko on...
Browser
Browser