Array to Function
In Java, “Array to function” refers to passing an array as an argument to a function. This allows the function to operate on the elements of the array. When you pass an array to a function, you are essentially giving that function access to the elements of the array, enabling it to perform operations like reading, modifying, or analysing those elements.
Structure:
public void yourFunctionName(int[] arrayName) {
// Your function code here
}
Scenario 1:
Write a Java program to create a method named displayCarParts that takes an array of strings as a parameter and prints each element of the array to the console.
The main method should create a string array named carParts and call the displayCarParts method with this array.
Java Code:
public class Program {
static void displayCarParts(String[] listOfParts) {
System.out.println(“Car parts:”);
for (String part : listOfParts) {
System.out.println(part);
}
}
public static void main(String[] args) {
String[] carPartList = {“Engine”, “Sensor”, “Starter”};
displayCarParts(carPartList);
}
}
Output:
Car parts:
Engine
Sensor
Starter
Explanation:
public class Program {
Defines a class named Program.
static void displayCarParts(String[] listOfParts)
Defines a static method that takes a string array as a parameter.
System.out.println(“Car parts:”);
Prints “Car parts:” to the console.
for (String part : listOfParts)
Iterates through each element in the carPartList array. For more details, please refer C# For Loop section in this Url : Java For loop
System.out.println(part);
Prints each car part to the console.
public static void main(String[] args)
Defines the main method, the entry point of the application.
String[] carPartList = {“Engine”, “Sensor”, “Starter”};
Creates and initializes an array of strings with the variable carPartList.
displayCarParts(carPartList);
Calls the displayCarParts method and passes the carPartList array as an argument to it.
Scenario 2:
Write a Java program that calculates and displays the sum of any three numbers using a method named sum.
The sum method should take an array of three integers and return their sum.
Java Code:
public class Program {
static int sum(int[] values) {
int sum = 0;
for (int number : values) {
sum += number;
}
return sum;
}
public static void main(String[] args) {
int[] values = {2, 4, 8};
int valueOfSum = sum(values);
System.out.println(valueOfSum);
}
}
Output:
14
Explanation:
public class Program
Defines a class named Program.
static int sum(int[] values)
Defines a static method that calculates the sum of elements in an integer array.
int sum = 0;
Defines a variable named sum and initialises it to 0.
for (int number : values)
Iterates through each element in the values array. For more details, please refer C# For Loop section in this Url : Click here
sum += number;
Adds each element number to sum.
return sum;
Returns the calculated sum.
public static void main(String[] args)
The entry point of the program.
int[] values = {2, 4, 8};
Defines an integer array named values with elements 2, 4, and 8.
int valueOfSum = sum(values);
Calls the sum method with values as the argument and stores the result in valueOfSum.
System.out.println(valueOfSum);
Prints the result (sum of array elements) to the console.
Tasks:
1. Sum of Elements:
Create a function SumArray that takes an array of integers and returns the sum of its elements.
Test the function by passing an array of integers and printing the result.
2. Average of Elements:
Create a function AverageArray that takes an array of doubles and returns the average of its elements.
Test the function by passing an array of doubles and printing the result.
3.Find Maximum:
Create a function FindMax that takes an array of integers and returns the maximum value.
Test the function by passing an array of integers and printing the result.
4. Count Occurrences:
Create a function CountOccurrences that takes an array of integers and a target integer, and returns the number of times the target integer occurs in the array.
Test the function by passing an array of integers and a target integer, and printing the result.
5. Filter Even Numbers:
Create a function FilterEvenNumbers that takes an array of integers and returns a new array containing only the even numbers from the original array.
Test the function by passing an array of integers and printing the result.
6. Calculate Median:
Create a function CalculateMedian that takes an array of integers and returns the median value.
Test the function by passing an array of integers and printing the result.
Course Video
YouTube Reference :
The training covers how to pass arrays to functions, manipulate them, and use Java’s Arrays
class for various operations.
Yes, it includes examples of passing single-dimensional and multi-dimensional arrays.
Yes, exercises include sorting, searching, and modifying arrays using methods.
Yes, the training is designed for beginners with step-by-step instructions and examples.
A Java IDE like Eclipse, IntelliJ IDEA, or an online compiler is sufficient.
Yes, it covers methods like sort()
, copyOf()
, and equals()
from the Arrays
class.
Yes, the training is self-paced and accessible online anytime.
Yes, it demonstrates how arrays passed to methods can be modified and affect the original array
Yes, this Java Arrays training is completely free on Iqra Technology Academy’s website.