Your SlideShare is downloading. ×
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Chapter 7.3
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Chapter 7.3

51

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
51
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. MANIPULATING ARRAY AS PARAMETERS TO METHODS Chapter 7.3:
  • 2. Arrays as Formal Parameters to Methods Arrays can be passed as parameter to methods Eg. public static void arrayAsFormalParameter(int[] listA, double[] listB, int num) { //… } Formal parameter The above method have 3 formal parameters – listA, listB and num
  • 3. continue Statement to call the method arrayAsFormalParameter(intList, doubleNumList, number); Actual parameter int[] intList = new int[10]; double[] doubleNumList = new double[15]; int number; Suppose we have the following statement
  • 4. example 1 public class PassingParameter { public static void main(String[] args) { int num[] = {10,20,30,40,50,60,70}; System.out.println(“ The number of elements: " + num.length); printArray(num); } public static void printArray(int[] number) { for (int index = 0; index < number.length; index++) System.out.println(number[index] + ""); } } Passing parameter OUTPUT: The number of elements: 7 10 20 30 40 50 60 70
  • 5. example 2 public static void main(String[] args) { int[] listA = {11,22,36,42,15,46,27,48,19,10} int[] listB = new int[10]; int Total, Largest; // call sumArray method and return a value to Total Total = sumArray (listA, listA.length); System.out.println(“n The sum of ListA is :” + Total); // call indexLargestElement and return the indux value to Largest indLargest = indexLargestElement (listA, list.length); System.out.println(“n The largest element is :” + listA[Largest]); continue
  • 6. public static int sumArray(int[] list, int noOfElements) { int index; int sum = 0; for (index = 0; index < noOfElement; index++) sum = sum + list[index]; return sum; } public static int indexLargestElement(int[] list, int noOfElement) { int index; int maxIndex = 0; for (index = 1; index < noOfElement; index++) if(list[maxIndex] < list[index]) maxIndex = index; return maxIndex; }
  • 7. Array of String Objects String[] nameList = new String[5] nameList[0] = “Amanda Green”; nameList[1] = “Vijay Arora”; nameList[2] = “Sheila Mann”; nameList[3] = “Rohit Sharma”; nameList[4] = “Mandy Johnson”;
  • 8. Array of Object  Can use arrays to manipulate objects.  Example: Create an array named array1 with N object of type T: T[] array1 = new T[N]  Can instantiate array1 as follows: for(int j=0; j < array1.length; j++) array1[j] = new T();  Eg: a) clock – hour, minute, second b) student – name, matric, age
  • 9. example import java.util.*; public class ArrayOfObj { int N = 3; StudentInfo[] student = new StudentInfo[N]; public static void main (String[] args) { int N = 3; int i; ArrayOfObj arr = new ArrayOfObj(); StudentInfo[] Std = new StudentInfo[N]; Std = arr.InputData(); arr.PrintInfo(Std); } Input students information's (name,matric, age) into array and print out the output class StudentInfo{ String name; String matric; int age; }
  • 10. public StudentInfo[] InputData() int i; StudentInfo[] student = new StudentInfo[N]; System.out.println("nEnter Students Information "); System.out.println("_______________________ ____ n"); for (i = 0; i< N; i++) { student[i] = new StudentInfo(); System.out.print("Name : "); student[i].name = input.readLine(); System.out.print("Matric No : "); student[i].matric = input.nextLine(); System.out.print("Age : "); student[i].age = input.nextInt(); System.out.println(); } return student; } public void PrintInfo(StudentInfo[] Std) { int i; System.out.println("List of students :n"); for (i=0;i<N;i++) { System.out.println((i+1) + ". " + Std[i].matric + " " + Std[i].name + " " + " " + Std[i].age); } }
  • 11. Output Enter Students Information ___________________________ Name : BAHARUDIN OSMAN Matric No : S11111 Age : 30 Name : BADRUL HAZMI Matric No : S23212 Age : 28 Name : NUR BADRINA Matric No : S34213 Age : 27 List of students : 1. S11111 BAHARUDIN OSMAN 30 2. S23212 BADRUL HAZMI 28 3. S34213 NUR BADRINA 27
  • 12. Statement below create an array of arrivalTimeEmp Clock[] arrivalTimeEmp = new Clock[100];
  • 13. Instantiating of Array Objects for (int j = 0; j < arrivalTimeEmp.length; j++) arrivalTimeEmp[j] = new Clock();
  • 14. Continue  Setting a time for index 49 arrivalTimeEmp[49].setTime(8, 5, 10);
  • 15. Delete Object  Step i. Identify the element to delete ii. Point the object to delete null iii. Move up all elements (after deleted object) iv. Point the last element to null
  • 16. for (i=0; i < student.length; i++) if(i==5) then student[i] = null null student Name Matric IC Name Matric IC Name Matric IC Step 1 : Identify the element to delete Step 2 : Point the object to delete to null - if the sixth element to delete
  • 17. Step 3 : Move up all elements (after deleted object) Step 4 : Point the last element to null element A element B element C element D element E element element G element H element I element J [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] element A element B element C element D element E element G element H element I element J [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] for (i = 0; i < student.length; i++) if (i= =5) student[i] = student[student.length -1) if (i= = (student.length – 1)) student[i] = null Set the last element to null nullbefore studentstudent after

×