Passing an Array to a Function in Java Last Updated : 13 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Passing an array to a function is an easy-to-understand task in Java. In this article, we will check how to pass an array as a method parameter.Caller Function Vs Called FunctionLet function GFG() be called from another function GFGNews(). Here, GFGNews is called the “Caller Function” and GFG is called the “Called Function OR Callee Function”. The arguments/parameters which GFGNews passes to GFG are called "Actual Parameters" and the parameters in GFG are called "Formal Parameters". The array to pass can be a one-dimensional(1D) array or a multi-dimensional array such as a 2D or 3D array. Syntax to Pass an Array as a Parameter1. Caller Functioncalled_function_name(array_name);The code for the called function depends on the dimensions of the array. The number of square brackets in the function prototype is equal to the dimensions of the array, i.e. [n] for 1D arrays, [n][n] for 2D arrays, [n][n][n] for 3D arrays, and so on.2. Called FunctionOne Dimensional ArrayreturnType functionName(datatype[] arrayName) { //statements}Two Dimensional ArrayreturnType functionName(datatype[][] arrayName) { //statements}Here:returnType: the return type of the called functionfunctionName: name of the called functiondatatype: the datatype of the arrayarrayName: name of the arrayNote: We can also write the function in the similar way for 1 and 2 Dimensional mentioned below.returnType functionName (datatype arrayName[])returnType functionName (datatype arrayName[][])Example to Pass an Array to a Function Java // Java Program to Pass an Array to a Function import java.io.*; class GFG { void function1(int[] a) { System.out.println("The first element is: " + a[0]); } void function2(int[][] a) { System.out.println("The first element is: " + a[0][0]); } public static void main(String[] args) { GFG obj = new GFG(); int[] arr1 = { 1, 2, 3, 4, 5 }; int[][] arr2 = { { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } }; // passing the 1D array to function 1 obj.function1(arr1); // passing the 2D array to function 2 obj.function2(arr2); } } OutputThe first element is: 1 The first element is: 10 Comment More infoAdvertise with us Next Article Passing an Array to a Function in Java himanshu20032002 Follow Improve Article Tags : Java Java-Arrays Practice Tags : Java Similar Reads How to Return an Array in Java? An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of 5 min read Convert List to Array in Java The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are give 5 min read How to Add an Element to an Array in Java? In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array. In this article, we will discuss the NewDifferent Ways to Add an Element to an ArrayThere are two different approaches we c 3 min read How to Declare an Array in Java? In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, dif 3 min read Array to ArrayList Conversion in Java In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th 3 min read Conversion of Array To ArrayList in Java Following methods can be used for converting Array To ArrayList: Method 1: Using Arrays.asList() method Syntax: public static List asList(T... a) // Returns a fixed-size List as of size of given array. // Element Type of List is of same as type of array element type. // It returns an List containing 5 min read Array getInt() Method in Java The java.lang.reflect.Array.getInt() is an inbuilt method in Java and is used to return an element at the given index from the specified Array as a int. Syntax Array.getInt(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The object array whose index is to 3 min read Difference between Array and String in Java An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value 5 min read System.arraycopy() in Java java.lang.System class provides useful methods for standard input and output, for loading files and libraries or to access externally defined properties. The java.lang.System.arraycopy() method copies a source array from a specific beginning position to the destination array from the mentioned posit 2 min read Array Declarations in Java (Single and Multidimensional) In Java, an Array is used to store multiple values of the same type in a single variable. There are two types of arrays in Java:Single-dimensional arraysMulti-dimensional arraysIn this article, we are going to discuss how to declare and use single and multidimensional arrays in Java.Single-Dimension 6 min read Like