Signup/Sign In

How to Return an Array in Java

In this article, we will learn about how to return an Array in Java. At the end of this tutorial, you will learn how to pass an array in Java Methods, How to perform an operation on them inside the method, and finally How to return an Array from the Java Methods.

Program to Return Array In Java

In a program given below, we created a method public static int[] getArray() which will return an array arr that assigned to arr which is declared in the method calling statement int[] arr = getArray(); and finally, the array will be printed. To return an array return type of function must be an array-type.

public class StudyTonight  
{  
	public static void main(String args[])  
	{  
		int[] arr=getArray();           
		for (int i = 0; i < arr.length; i++) 
			System.out.print( arr[i]+ " ");     
	}  
	public static int[] getArray()  
	{  
		int[] arr={17,18,19,20,21};   
		return arr;  
	}  
}  


17 18 19 20 21

As the above code works correctly, let's add one more functionality of how to pass an array to an element. Now, we will implement a program where a method will accept an array and return a reverse of an array.

Example: Return Array In Java and Passing Array as an Argument

In the below program, we passed an Array inside the method reverseArray() then we reversed an array and returned it using the return keyword and after that, it is assigned to the original array.

public class StudyTonight  
{  
	public static void main(String args[])  
	{  
		int[] arr={17,18,19,20,21};   
		//calling reverse method to reverse an array
		arr=reverseArray(arr); 
		//printing an array
		for (int i = 0; i < arr.length; i++) 
			System.out.print( arr[i]+ " ");     
	}  
	public static int[] reverseArray(int arr[])  
	{  
		int end = arr.length - 1;
		int start = 0;
		//logic for reverse of an Array
		while (start < end)
		{
			int temp = arr[start]; 
			arr[start] = arr[end];
			arr[end] = temp;
			start++;
			end--;
		} 
		//returning an reversed array
		return arr;  
	}  
}  


21 20 19 18 17

Example: Returning an array of objects from a function

In the program given below, we will return an array of objects from a function. We can see we have a class Student in which stud_name and roll_no are data members. When we are calling getStudents() method, it will create an array of Student class and return it to the Student[] arr after that, we are printing the data for each object.

class Student
{
	String stud_name;
	int roll_no;
	//constructor for initializing data members
	Student(String stud_name, int roll_no)
	{
		this.stud_name = stud_name;
		this.roll_no = roll_no;
	}
}
public class StudyTonight  
{  
	public static void main(String args[])  
	{  
		Student[] arr = getStudents();
		//print data of each object
		for(Student obj:arr)
		{
			System.out.println("Name: "+obj.stud_name+" Roll No: "+obj.roll_no);
		}
	}  
	public static Student[] getStudents()  
	{
		Student[] arr= new Student[2];
		arr[0] = new Student("ABC",123);
		arr[1] =new Student("DEF",456);
		//return an array of objects of Student class
		return arr;		 
	}  
}  


Name: ABC Roll No: 123
Name: DEF Roll No: 456

Conclusion

To return an array we use a return statement inside the function and the return type of a function must be of an array type. It is very handy when we want to return a group of data in Java Methods.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.