To return an array of long values from a method, which return type should be used for the method?

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.

Example

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

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]

To return an array of long values from a method, which return type should be used for the method?

Updated on 30-Jul-2019 22:30:20

Can anybody explain what should this type of a method return?

public static List<long[]> removNb(long input) {

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 {
    public static int[] rtnarray() {
        int[] ary = {0,2,4,6,8,10,12,14,16,18,20};
        return ary;
    }
    public static void main(String[] args) {
        int[] get = rtnarray();
        int q = 0;
        while(q<get.length)
        {
            System.out.println("The value at index-number " + q + " is: " +get[q]);
            q++;
        }
}
}

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:

To return an array of long values from a method, which return type should be used for the method?

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.

Conclusion

In 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.

In this section, we are going to learn how to return an array in Java.

Remember:

  • A method can return a reference to an array.
  • The return type of a method must be declared as an array of the correct data type.

Example 1

In the following example, the method returns an array of integer type.

import java.util.Arrays; public class ReturnArrayExample1 { public static void main(String args[]) { int[] a=numbers(); //obtain the array for (int i = 0; i < a.length; i++) //for loop to print the array System.out.print( a[i]+ " "); } public static int[] numbers() { int[] arr={5,6,7,8,9}; //initializing array return arr; } }

Output:

To return an array of long values from a method, which return type should be used for the method?

Example 2

In the following example, the method returns an array of double type.

public class ReturnArrayExample3 { 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]+ " "); } }

Output:

To return an array of long values from a method, which return type should be used for the method?

Example 3

In the following example, method returns an array of object type.

import java.util.Arrays; class ReturnArrayExample3 { public static int[] returnArray() { int a1=20; int a2=23; int a3=87; return new int[] {a1,a2,a3}; //returns array } public static void main(String args[]) { int[] arr=returnArray(); //invoking method and storing returned array into arr System.out.println(Arrays.toString(arr)); //returns the string representation of the object } }

Output:

To return an array of long values from a method, which return type should be used for the method?

Next TopicJava Tutorial

To return an array of long values from a method, which return type should be used for the method?
For Videos Join Our Youtube Channel: Join Now

  • Send your Feedback to [email protected]
To return an array of long values from a method, which return type should be used for the method?
To return an array of long values from a method, which return type should be used for the method?
To return an array of long values from a method, which return type should be used for the method?