Download presentation
Presentation is loading. Please wait.
1
1 More on 1-D Array Overview l Array as a return type to methods l Array of Objects l Array Cloning l Preview: 2-D Arrays
2
2 Array as return type to methods l We saw in the last lecture, that arrays can be passed as parameters to methods. l In addition, methods can return array as their result a reference to an array object. import java.io.*; class ArrayAndMethods { static BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int size; System.out.print("Enter array size: "); size = Integer.parseInt(stdin.readLine()); double[] firstArray = createArray(size); double[] secondArray = createArray(size); System.out.println("The dot product = "+dotProduct(firstArray, secondArray)); }
3
3 Array as return type to methods static double[] createArray(int size) throws IOException { double[] array = new double[size]; System.out.println("Enter "+size+" elements for this array: "); for (int i=0; i<size; i++) array[i] = Double.parseDouble( stdin.readLine()); return array; } static double dotProduct(double[] first, double[] second) { double sum = 0; for (int i=0; i<first.length; i++) sum += first[i]*second[i]; return sum; }
4
4 Array of Objects l In the examples we have seen so far, we have been creating arrays whose elements are primitive types. l We can equally create an array whose elements are objects. l Assuming we have a class named Student, then we can create an array, students, to hold 10 Student objects as follows: Student[] students = new Student[10];
5
5 Array of Objects (cont’d) l As you can see from the figure, there is a fundamental difference between an array of primitive type and an array of object. l Here, the array only holds references to the actual objects. l The statement: Student[] students = new Student[10]; only creates the references. To actually create the objects, we have to use the new operator, usually is a loop as follows: for (int i = 0; i < students.length ; i++) students[i] = new Student(id, grade); l The following is the complete Student example. class Student { int iDNumber; double grade; public Student(int iDNumber, double grade) { this.iDNumber = iDNumber; this.grade = grade; } public void print() { System.out.println(iDNumber+"\t"+grade); }
6
6 Array of Objects (cont’d) import java.io; public class ArrayOfObjects { static BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int size; System.out.print("Enter number of students: "); size = Integer.parseInt(stdin.readLine()); Student[] students = createArray(size); double average = average(students); System.out.println("The average is "+average); System.out.println("Students below average are"); for (int i=0; i<students.length; i++) if (students[i].grade < average) students[i].print(); }
7
7 Array of Objects (cont’d) static Student[] createArray(int size) throws IOException { Student[] array = new Student[size]; int id; double grade; System.out.println("Enter "+size+" students"); for (int i=0; i<size; i++) { System.out.print("ID Number : "); id = Integer.parseInt(stdin.readLine()); System.out.print("Grade : "); grade = Double.parseDouble( stdin.readLine()); array[i] = new Student(id, grade); } return array; } static double average(Student[] studentList) { double sum = 0; for (int i=0; i<studentList.length; i++) sum += studentList[i].grade; return sum/studentList.length; }
8
8 Array cloning l The following example shows how we may make a copy of an array. This is called clonning. class ArrayCloningExample { public static void main (String[] args) { int[] a = new int[5]; int[] b; int i; System.out.println("Contents of a[]"); for(i = 0; i < a.length; i++) { a[i] = i * i; System.out.println("i = "+i+" a["+i+"]=\t"+ a[i]); } b = (int []) a.clone(); System.out.println("Contents of b[] immediately after duplication from a[]"); for(i = 0; i < b.length; i++) System.out.println("i = "+i+" b[" +i+ "] =\t" + b[i]); for(i = 0; i < b.length; i++) b[i] *= 2; System.out.println("Contents of a[] and b[] after doubling elements of b[]");
9
9 Array cloning (cont’d) for(i = 0; i < b.length; i++) System.out.println("i = " + i + " a[" + i + "] =\t"+a[i]+"\t\tb[" + i + "] =\t" + b[i]); } Sample Output: Contents of a[] i = 0 a[0] = 0 i = 1 a[1] = 1 i = 2 a[2] = 4 i = 3 a[3] = 9 i = 4 a[4] = 16 Contents of b[] after duplication from a[] i = 0 b[0] = 0 i = 1 b[1] = 1 i = 2 b[2] = 4 i = 3 b[3] = 9 i = 4 b[4] = 16 Contents of a[] and b[] after doubling elements of b[] i = 0 a[0] = 0 b[0] = 0 i = 1 a[1] = 1 b[1] = 2 i = 2 a[2] = 4 b[2] = 8 i = 3 a[3] = 9 b[3] = 18 i = 4 a[4] = 16 b[4] = 32
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.