Introduction
In Java, we can return an array from a method. This is useful when we want to return a group of data. In this lab, we will learn how to return an array in Java.
In Java, we can return an array from a method. This is useful when we want to return a group of data. In this lab, we will learn how to return an array in Java.
To return an array, the return type of the method must be an array type. In the following example, we declare a method named getArray()
that returns an array of integers.
public static int[] getArray() {
int[] arr = {1, 2, 3, 4, 5};
return arr;
}
To call the method that returns an array, assign the returned array to a variable of the same type, as shown below:
int[] myArray = getArray();
To print the array returned from the method, you can use a for loop to iterate over the elements of the array and print them one by one:
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
We can also pass an array to a method and perform operations on it inside the method, then return the modified array. In the following example, we pass an array of integers to a method named reverseArray()
that reverses the order of the elements in the array and returns the reversed array.
public static int[] reverseArray(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
return arr;
}
To call the method that reverses the array and return the modified array, first, create an array, then call the method reverseArray()
and pass the created array as a parameter. Finally, to print the modified array, use a for loop to iterate over the elements of the array and print them one by one:
int[] myArray = {1, 2, 3, 4, 5};
myArray = reverseArray(myArray);
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
We can also return an array of objects from a method. In the following example, we create a Student
class with stud_name
and roll_no
data members. Then, we declare a method named getStudents()
that returns an array of Student
objects.
class Student
{
String stud_name;
int roll_no;
Student(String stud_name, int roll_no) {
this.stud_name = stud_name;
this.roll_no = roll_no;
}
}
public static Student[] getStudents() {
Student[] arr = new Student[2];
arr[0] = new Student("Alice", 101);
arr[1] = new Student("Bob", 102);
return arr;
}
To call the method that returns an array of objects, assign the returned array to a variable of the same type, then use a for loop to iterate over the elements of the array and print the contents of each object.
Student[] myArray = getStudents();
for (int i = 0; i < myArray.length; i++) {
System.out.println("Name: " + myArray[i].stud_name + " Roll No: " + myArray[i].roll_no);
}
In this lab, we learned how to return an array in Java. We also learned how to pass an array to a method, perform an operation on it, and return the modified array. Additionally, we learned how to return an array of objects from a method.