MANIPULATING ARRAY AS
PARAMETERS TO METHODS
Chapter 7.3:
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
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
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
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
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;
}
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”;
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
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;
}
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);
}
}
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
Statement below create an array of
arrivalTimeEmp
Clock[] arrivalTimeEmp = new Clock[100];
Instantiating of Array
Objects
for (int j = 0; j < arrivalTimeEmp.length; j++)
arrivalTimeEmp[j] = new Clock();
Continue
 Setting a time for index 49
arrivalTimeEmp[49].setTime(8, 5, 10);
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
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
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

More Related Content

PPTX
Row major and column major in 2 d
PPT
Arrays and structures
PPT
PDF
PDF
Two dimensional array
PDF
Numpy python cheat_sheet
PDF
11 1. multi-dimensional array eng
PPT
Data Structure and Algorithms Arrays
Row major and column major in 2 d
Arrays and structures
Two dimensional array
Numpy python cheat_sheet
11 1. multi-dimensional array eng
Data Structure and Algorithms Arrays

What's hot (20)

PPTX
Set data structure
PPTX
Java arrays
PPT
Sparse Matrix and Polynomial
PDF
9 python data structure-2
PPTX
Python array
PPT
Multi dimensional arrays
PPTX
Array operations
PPT
02 Arrays And Memory Mapping
PDF
Arrays in python
PPT
Arrays Data Structure
PDF
Numpy tutorial(final) 20160303
PPT
Chapter 3 ds
PPTX
Data structures
PPT
Scilab - Piecewise Functions
PPTX
Java Foundations: Lists, ArrayList<T>
PDF
Multi dimensional array
PDF
Arrays in C
PDF
Python programming : Arrays
PDF
Array linear data_structure_2 (1)
PPTX
Java Foundations: Maps, Lambda and Stream API
Set data structure
Java arrays
Sparse Matrix and Polynomial
9 python data structure-2
Python array
Multi dimensional arrays
Array operations
02 Arrays And Memory Mapping
Arrays in python
Arrays Data Structure
Numpy tutorial(final) 20160303
Chapter 3 ds
Data structures
Scilab - Piecewise Functions
Java Foundations: Lists, ArrayList<T>
Multi dimensional array
Arrays in C
Python programming : Arrays
Array linear data_structure_2 (1)
Java Foundations: Maps, Lambda and Stream API
Ad

Viewers also liked (9)

PPTX
Chapter 4.1
PPTX
Chapter 6.5 new
PPTX
Chapter 8.3
PPTX
Chapter 5.2
PPTX
Chapter 6.4
PPTX
Chapter 2.1
PPTX
Chapter 7.0
PPTX
Chapter 2.4
PPTX
Chapter 6.2
Chapter 4.1
Chapter 6.5 new
Chapter 8.3
Chapter 5.2
Chapter 6.4
Chapter 2.1
Chapter 7.0
Chapter 2.4
Chapter 6.2
Ad

Similar to Chapter 7.3 (20)

PPTX
Chapter 13.pptx
PPT
Java căn bản - Chapter10
PDF
Aj unit2 notesjavadatastructures
PPTX
6_Array.pptx
PPTX
Arrays in Java with example and types of array.pptx
PPT
ch07-arrays.ppt
PPT
Oop lecture7
PPT
Topic20Arrays_Part2.ppt
PPTX
Chapter 7.2
PPTX
Lecture_3.5-Array_Type Conversion_Math Class.pptx
PPTX
Chap1 array
PDF
Class notes(week 4) on arrays and strings
PPT
Fp201 unit4
PPTX
Data Structures and Agorithm: DS 02 Array List.pptx
PPTX
Chapter 7.1
PPTX
07+08slide.pptx
PDF
Data Structure Lecture Array and Recursion.pdf
PPT
17-Arrays en java presentación documento
PPTX
Mixing functional programming approaches in an object oriented language
PDF
Write a method called uniqueNumbers that takes an int array as param.pdf
Chapter 13.pptx
Java căn bản - Chapter10
Aj unit2 notesjavadatastructures
6_Array.pptx
Arrays in Java with example and types of array.pptx
ch07-arrays.ppt
Oop lecture7
Topic20Arrays_Part2.ppt
Chapter 7.2
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Chap1 array
Class notes(week 4) on arrays and strings
Fp201 unit4
Data Structures and Agorithm: DS 02 Array List.pptx
Chapter 7.1
07+08slide.pptx
Data Structure Lecture Array and Recursion.pdf
17-Arrays en java presentación documento
Mixing functional programming approaches in an object oriented language
Write a method called uniqueNumbers that takes an int array as param.pdf

More from sotlsoc (20)

PPTX
Chapter 2.0 new
PPTX
Chapter 1.0 new
PPTX
Chapter 3.0 new
PPTX
Chapter 11.1
PPTX
Chapter 10.3
PPTX
Chapter 11.4
PPTX
Chapter 11.3
PPTX
Chapter 11.5
PPTX
Chapter 11.2
PPTX
Chapter 11.0
PPTX
Chapter 10.2
PPTX
Chapter 10.1
PPTX
Chapter 8.0
PPTX
Chapter 10.0
PPTX
Chapter 9.3
PPTX
Chapter 9.4
PPTX
Chapter 9.1
PPTX
Chapter 8.1
PPTX
Chapter 9.0
PPTX
Chapter 9.2
Chapter 2.0 new
Chapter 1.0 new
Chapter 3.0 new
Chapter 11.1
Chapter 10.3
Chapter 11.4
Chapter 11.3
Chapter 11.5
Chapter 11.2
Chapter 11.0
Chapter 10.2
Chapter 10.1
Chapter 8.0
Chapter 10.0
Chapter 9.3
Chapter 9.4
Chapter 9.1
Chapter 8.1
Chapter 9.0
Chapter 9.2

Recently uploaded (20)

PPTX
2025 High Blood Pressure Guideline Slide Set.pptx
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI Syllabus.pdf
PDF
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
PDF
Nurlina - Urban Planner Portfolio (english ver)
PDF
Solved Past paper of Pediatric Health Nursing PHN BS Nursing 5th Semester
PDF
0520_Scheme_of_Work_(for_examination_from_2021).pdf
PPTX
Climate Change and Its Global Impact.pptx
PPTX
Thinking Routines and Learning Engagements.pptx
PDF
Compact First Student's Book Cambridge Official
PPTX
What’s under the hood: Parsing standardized learning content for AI
PPT
REGULATION OF RESPIRATION lecture note 200L [Autosaved]-1-1.ppt
PDF
African Communication Research: A review
DOCX
Ibrahim Suliman Mukhtar CV5AUG2025.docx
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
THE CHILD AND ADOLESCENT LEARNERS & LEARNING PRINCIPLES
PPTX
UNIT_2-__LIPIDS[1].pptx.................
PDF
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
2025 High Blood Pressure Guideline Slide Set.pptx
Literature_Review_methods_ BRACU_MKT426 course material
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI Syllabus.pdf
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
Nurlina - Urban Planner Portfolio (english ver)
Solved Past paper of Pediatric Health Nursing PHN BS Nursing 5th Semester
0520_Scheme_of_Work_(for_examination_from_2021).pdf
Climate Change and Its Global Impact.pptx
Thinking Routines and Learning Engagements.pptx
Compact First Student's Book Cambridge Official
What’s under the hood: Parsing standardized learning content for AI
REGULATION OF RESPIRATION lecture note 200L [Autosaved]-1-1.ppt
African Communication Research: A review
Ibrahim Suliman Mukhtar CV5AUG2025.docx
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
THE CHILD AND ADOLESCENT LEARNERS & LEARNING PRINCIPLES
UNIT_2-__LIPIDS[1].pptx.................
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...

Chapter 7.3

  • 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