chapter 7.3
Post on 04-Jul-2015
102 Views
Preview:
TRANSCRIPT
MANIPULATING ARRAY AS
PARAMETERS TO METHODS
Chapter 7.3:
Arrays as Formal Parameters to
MethodsArrays 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 ObjectsString[] 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);
}
}
OutputEnter 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
Objectsfor (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
top related