How to Return an Array in Java?

We can return an array in java using a different method such as createArray() which creates a dynamic array and returns its value.

An Array is returned from methods where you need to return multiple values of the same type.

The data type that returns the value should specify an array of suitable data types. Since primitive types can be returned from Java programs, we can return a reference to the array as primitive.

But remember the method can return a reference to an array and the return type of a method should be declared as an array of the correct data type.

Now let us see how to program is written to return an array in java.

1. How to Return an Integer in Java Using createArray() Method

After executing the below program, the compiler will ask the user to enter the size of an array. After entering the size the compiler will ask to enter the elements and then returns the elements of an array on the screen.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
import java.util.Arrays;
import java.util.Scanner;
public class Learnprogramo
{
public int[] createArray() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array that is to be created:: ");
int size = sc.nextInt();
int[] myArray = new int[size];
System.out.println("Enter the elements of the array ::");
for(int i=0; i<size; i++)
{
myArray[i] = sc.nextInt();
}
return myArray;
}
public static void main(String args[]) {
Learnprogramo obj = new Learnprogramo();
int arr[] = obj.createArray();
System.out.println("Array created is :: "+Arrays.toString(arr));
}
}</pre>
</div>

Output:

how to return an array in java

The above program returns an array of integer type.

2. If return type is Double

In this program, we will see how to write a java program to return an array if the returned value is double.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
public class Learnprogramo 
{  
public static double[] returnArray()  
{  
double[] arr = new double [3];// Creating an array of 3 elements  
arr[0]=6.9;  
arr [1]=2.5;  
arr [2]=11.5;  
return(x);// Return the reference of the array  
}  
public static void main(String[] args)  
{  
double[] a; //variable to store returned array  
a = returnArray();  //called method  
for (int i = 0; i < a.length; i++) //for loop to print the array  
System.out.println( a[i]+ " ");     
}  
}  </pre>
</div>

Output:

how to return an array in java

3. If Method Returns an Array of Object Type

In this program, we will see if the method returns an array of the Object type.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
import java.util.Arrays;  
class Learnprogramo  
{  
public static int[] returnArray()  
{  
int a1=20;  
int a2=23;  
int a3=87;  
return new int[] {a1,a2,a3};
}  
public static void main(String args[])  
{  
int[] arr=returnArray();
System.out.println(Arrays.toString(arr)); 
}  
}  </pre>
</div>

Output:

how to return an array in java

4. Returning Multiple Values in Java

In this program, the method can return multiple values but of the same type by returning an array.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
public class Main 
{ 
static int[] getSumAndSub(int a, int b) 
{ 
int[] ans = new int[2]; 
ans[0] = a + b; 
ans[1] = a - b; 
// returning array of elements 
return ans; 
} 
public static void main(String[] args) 
{ 
int[] ans = getSumAndSub(175, 25); 
System.out.println("Sum = " + ans[0]); 
System.out.println("Sub = " + ans[1]); 
} 
} </pre>
</div>

Output:

how to return an int in java

5. How to Return String Array From Method

The following program demonstrates an example of returning an array reference from the method. To declare an array of string return_Array() method is declared and ret_Array is used to return an array.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
import java.util.*;
public class Main
{
public static String[] return_Array() 
{
//define string array
String[] ret_Array = {"India", "UK", "USA", "Spain", "France"};
//return string array
return ret_Array;
}
public static void main(String args[]) {
//call method return_array that returns array   
String[] str_Array = return_Array();
System.out.println("Array returned from method:" + Arrays.toString(str_Array));
}
}</pre>
</div>

Output:

using object

Frequently Asked Questions?


1. How to Return an Int Array in Java?

We can return an int array in java using createArray() method which accepts integers from the users and return an integer array.

2. How to Return the Index of an Array in Java?

The java provides an inbuilt method called as IndexOf() which returns an index of the first appearance of the target in the array. lastIndexOf() method is also used to return the index of the last appearance of the target in the array.

3. How to Return an array from a Method in Java?

Java inbuilt method createArray() is used to return an array where we have to input array elements in the program.

What is an Array in Java?

In Java, arrays are a basic construct that allows you to conveniently store and access a large number of values. There are containers that contain data values of a single type.

For example, you can create an array that can contain 100 values for the int type.

The size of an array is also known as the length of the array and is specified when creating the arrays. 

The total number of values is the size of the array and the values are stored directly in the arrays starting from index 0. The array can store up to 5 values of an integer data type (int array = new int (5)). Fix that “5 values” means that the value is stored directly in an array, starting with an index of 0 and so on.

An array is a Java object, and every variable declared in it contains a reference to that object, just like any other variable in the same class. 

Note that this declaration only names the variable and says that it references the type of the array.

This shows that the array is of its kind: it is a Java object, just like any other object in the class.

Array as Java Object

The array is a Java object, and every variable declared in it contains a reference to that object, just like any other variable in the class, such as the array variable.

The array can contain any number of elements, and the size of the array is bracketed with a square.

The next statement in the ArrayDemo program assigns an array with the new keyword, followed by a space and then a type.

In the Java programming language, a multi-dimensional array is an array that is itself a component of another array, such as a single, multi-dimensional array. 

Finally, the built-in length property can use the size of the array to determine its size as well as its length.

Variables in Java Array

Variables in Java arrays can be declared as data types like all other variables. As in C and C + +, the size of an array can be specified using the size property.

Collecting the same object type and sending values in a single method call is also useful in real-time projects.

Collections of this type in Java are part of the Java package and most of them function as a collection of data types such as arrays, arrays of objects, and value collections.

The declaration of an array specifies the type of elements the array contains, followed by an identifier with square brackets indicating its array type.

Since the size must be specified in a new keyword that follows the keywords of a keyword – word in the declaration of an object (e.g. array, array _ type, etc.), the JVM must also know what size of space is reserved for each object.

Also Read: