Java 8Object Oriented ProgrammingProgramming 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. ExampleLive Demo 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)); } }OutputEnter 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]
Updated on 30-Jul-2019 22:30:20
Can anybody explain what should this type of a method return? I've been struggling with this one for a long time. In my method, this ArrayList List<Long>result=new ArrayList<>(); holds the return data, but I can't understand what should return look like as if I try to return, it throws an error error: incompatible types: List<Long> cannot be converted to List<long[]> .I've searched the web but did not find anything. I've never seen this List<Long[]> before. What are these [] braces after long? I can't change function prototype as my exercise conditions do not permit it. I've tried creating long array and using Arrays.asList method but it did not help or I used it wrong. Hope you guys could help me. Thank you in advance. Peace and love!
As we know that arrays are very important for a programming language as they group the values of same data type in one variable so in Java array also play a vital role. When we create functions or methods we usually pass variables as arguments. But what if we want to return a large amount of data having same data type at once from a function or method? We can do that by returning an array where we want to use numerous values of the same data type without occupying the large memory space. How do we return an array in Java?In Java, we can return an array from a function. The following practical example, will showcase how to return an array practically in Java. Code: public class arry { In this code, we create a static function which will return an integer type array. Then in the main function, we create an integer type array variable and initialize it with the function that returns an integer array. Lastly, we use a while loop to display the elements of the array. Output: The output clearly shows that we can return an array with the help of a method and display the required result. Here you go! You have learned to return an array in Java. ConclusionIn java, an array can be returned with the help of a method or a function. For this purpose, the method return type must be the type of the array, and the variable that stores the array also has the same data type as the array. In this article, we talked about we have gone through the prose in detail through which we can return an array in Java.
|