How To Return An Array in Java
How To Return An Array in Java
Esha Gupta
Asso ciate Senio r Executive
Arrays in Java play a fundamental role in storing and managing collections of similar
data types. A common practice when working with methods is to return an array,
allowing dynamic data to be organized and easily transferred between parts of a
program. This blog will help you learn “How to return an array in Java”!
Table of Content
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
How to Return an Array from a Method?
Syntax
Real-Life Example
Conclusion
Synt ax
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
public static dataT ype[] methodName() {
// ... method body
return arrayName;
}
Where dataT ype can be any valid data type in Java, like int , double , String , etc.
To return an array, you declare the method’s return type as the array type and then
use the return statement to return the array.
Example
Copy code
Output
12345
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
The code prints 1 2 3 4 5 which confirms that it is returning and processing a one-
dimensional array.
Example
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Output
123
456
789
This confirms the code returns and processes a two-dimensional array (matrix).
If the array data shouldn’t be modif ied, consider returning a cloned version or using
collections that of f er immutability.
Returning null: T his can lead to NullPointerException. Ensure you always return a valid
array or handle null scenarios.
Modifying Returned Arrays: Since arrays are objects, changes in returned arrays can
af f ect the original. Always be cautious when modif ying arrays returned by methods.
Real-Lif e Example
Problem Statement:
Imagine you are building a system for a bookstore. This system needs a feature
that, given a list of books and authors, can return an array of book titles by a
specific author.
Query the inventory to f ind all books by a specif ic author, which will be returned as an
array of titles.
Requirements:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Create a Book class that represents a book, with attributes f or the title and author.
Create a BookStore class that represents the bookstore, with methods to add a book
and to get books by author.
Code
Copy code
class Book {
privat e St ring t it le;
privat e St ring aut hor;
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
public Book(St ring t it le, St ring aut hor) {
t his.t it le = t it le;
t his.aut hor = aut hor;
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
}
Output
How It Works:
T he Book class is used to represent a book, with title and author as attributes.
In the Main class, we add books to the BookStore and then query the store f or books by
George Orwell, which will return an array of book titles.
This example effectively shows how to return an array in Java, aligning with a
realistic software product requirement of handling a bookstore inventory and
providing book search functionality based on the author’s name.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Underst anding Dat a St ruct ures and Algorit hms in Java
Data Structure in Java is used to sto re and o rganize data efficiently while the
algo rithms are used to manipulate the data in that structure. In this article, we will
briefly...re ad m o re
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
/**
* Generates an array of a given size, starting with a specified start value.
*
* @param size the size of the array to be generated
* @param startValue the starting value of the array
* @return an array of integers
*/
public st at ic int [] generat eDynamicArray(int size, int st art Value) {
int [] result = new int [size];
f or (int i = 0; i < size; i++) {
result [i] = st art Value + i;
}
ret urn result ;
}
}
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
10 11 12 13 14
Copy code
Output
Sunday
Monday
T uesday
Wednesday
T hursday
Friday
Saturday
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
class Person {
privat e St ring name;
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Output
Rekha
Granth
Neeraj
Copy code
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
1
23
456
Arrays in Java can be “jagged”, meaning they can have rows of different lengths.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Output
40
10
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
60
Copy code
// Trying to print elements (This loop won't run since the array is empty)
f or (St ring st r : result Array) {
Syst em.out .print ln(st r);
}
}
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Output
a
b
c
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Output
0
10
20
30
40
50
60
Conclusion
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
The ability to return arrays in Java enhances modularization, code reusability, and
data encapsulation. Whether you’re a beginner or an expert, this article will definitely
help you to clear the concept of returning arrays in Java. Keep learning, Keep
exploring!
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.