SlideShare uma empresa Scribd logo
1 de 17
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

Mais conteúdo relacionado

Mais procurados

Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and PolynomialAroosa Rajput
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
 
Array operations
Array operationsArray operations
Array operationsZAFAR444
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory MappingQundeel
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonLifna C.S
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structurestudent
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Namgee Lee
 
Scilab - Piecewise Functions
Scilab - Piecewise FunctionsScilab - Piecewise Functions
Scilab - Piecewise FunctionsJorge Jasso
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Svetlin Nakov
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional arrayRajendran
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)eShikshak
 
Java Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APIJava Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APISvetlin Nakov
 

Mais procurados (20)

Set data structure
Set data structure Set data structure
Set data structure
 
Java arrays
Java   arraysJava   arrays
Java arrays
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Python array
Python arrayPython array
Python array
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Array operations
Array operationsArray operations
Array operations
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
Chapter 3 ds
Chapter 3 dsChapter 3 ds
Chapter 3 ds
 
Data structures
Data structuresData structures
Data structures
 
Scilab - Piecewise Functions
Scilab - Piecewise FunctionsScilab - Piecewise Functions
Scilab - Piecewise Functions
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Java Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APIJava Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream API
 

Destaque

Chapter 4.1
Chapter 4.1Chapter 4.1
Chapter 4.1sotlsoc
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 newsotlsoc
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3sotlsoc
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2sotlsoc
 
Chapter 6.4
Chapter 6.4Chapter 6.4
Chapter 6.4sotlsoc
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1sotlsoc
 
Chapter 7.0
Chapter 7.0 Chapter 7.0
Chapter 7.0 sotlsoc
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4sotlsoc
 
Chapter 6.2
Chapter 6.2Chapter 6.2
Chapter 6.2sotlsoc
 

Destaque (9)

Chapter 4.1
Chapter 4.1Chapter 4.1
Chapter 4.1
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2
 
Chapter 6.4
Chapter 6.4Chapter 6.4
Chapter 6.4
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
Chapter 7.0
Chapter 7.0 Chapter 7.0
Chapter 7.0
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
 
Chapter 6.2
Chapter 6.2Chapter 6.2
Chapter 6.2
 

Semelhante a Chapter 7.3

Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10Vince Vo
 
Aj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresAj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresArthik Daniel
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.pptMahyuddin8
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptadityavarte
 
Chapter 7.2
Chapter 7.2Chapter 7.2
Chapter 7.2sotlsoc
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxShahinAhmed49
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1sotlsoc
 
Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMark Needham
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdffashioncollection2
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...Nithin Kumar,VVCE, Mysuru
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfRohitkumarYadav80
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................suchitrapoojari984
 
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdfAnkitchhabra28
 

Semelhante a Chapter 7.3 (20)

Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
Aj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresAj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructures
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Chapter 7.2
Chapter 7.2Chapter 7.2
Chapter 7.2
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented language
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
 

Mais de sotlsoc

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

Mais de sotlsoc (20)

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

Último

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Último (20)

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

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