- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to return an array from a method in Java?
We can return an array in Java from a method in Java. Here we have a method createArray() from which we create an array dynamically by taking values from the user and return the created array.
Example
import java.util.Arrays; import java.util.Scanner; public class ReturningAnArray { 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[]) { ReturningAnArray obj = new ReturningAnArray(); int arr[] = obj.createArray(); System.out.println("Array created is :: "+Arrays.toString(arr)); } }
Output
Enter the size of the array that is to be created:: 5 Enter the elements of the array :: 23 47 46 58 10 Array created is :: [23, 47, 46, 58, 10]
- Related Articles
- How to return 2 values from a Java method
- Returning an Array from a Method in Java
- How to return an array from a function in C++?
- Can we return this keyword from a method in java?
- How to create an ArrayList from an Array in Java?
- How to remove an element from an array in Java
- C# program to return an array from methods
- How to switch data from an array to array list in java?
- How to return local array from a C++ function?
- MongoDB query to return specific fields from an array?
- Haskell Program to return an array from the function
- How to return custom result type from an action method in C# ASP.NET WebAPI?
- How to create LabelValue Tuple from an array in Java
- How to pull distinct values from an array in java?
- How to return an object from a function in Python?

Advertisements