How to Return an Array

JavaJavaBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/BasicSyntaxGroup -.-> java/while_loop("`While Loop`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117450{{"`How to Return an Array`"}} java/classes_objects -.-> lab-117450{{"`How to Return an Array`"}} java/modifiers -.-> lab-117450{{"`How to Return an Array`"}} java/oop -.-> lab-117450{{"`How to Return an Array`"}} java/identifier -.-> lab-117450{{"`How to Return an Array`"}} java/arrays -.-> lab-117450{{"`How to Return an Array`"}} java/data_types -.-> lab-117450{{"`How to Return an Array`"}} java/for_loop -.-> lab-117450{{"`How to Return an Array`"}} java/operators -.-> lab-117450{{"`How to Return an Array`"}} java/output -.-> lab-117450{{"`How to Return an Array`"}} java/strings -.-> lab-117450{{"`How to Return an Array`"}} java/variables -.-> lab-117450{{"`How to Return an Array`"}} java/while_loop -.-> lab-117450{{"`How to Return an Array`"}} java/system_methods -.-> lab-117450{{"`How to Return an Array`"}} end

Declare a method that returns an array.

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;
}

Call the method.

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();

Print the returned array.

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] + " ");
}

Pass an array to a method and return a modified array.

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;
}

Call the method and print the modified array.

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] + " ");
}

Return an array of objects from a method.

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;
}

Call the method and print the objects.

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);
}

Summary

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.

Other Java Tutorials you may like