1, Overview
In this tutorial, We'll learn how to return an array from a method in java.
If you are new to java programming, please refer to the article on How to initialize an array in java?
Thump rules:
- A method can return an array that is referencing another array.
- The return type of method must be declared as the correct array type.
2. Java Return Array from Method - int[] primitive array
In the below example, getNumbers() method returns an integer primitive type array.
Example 1:
package com.javaprogramto.arrays.returns; import java.util.Arrays; public class ArrayReturnExample1 { public static void main(String[] args) { int[] first5Numbers = getNumbers(); System.out.println("Array returned from method : " + Arrays.toString(first5Numbers)); } // returning an array from method public static int[] getNumbers() { int[] numbers = { 1, 2, 3, 4, 5 }; return numbers; } }
Output:
Array returned from method : [1, 2, 3, 4, 5]
3. Java Return Array - double[] primitive & wrapper array
Below example returns a double[] array from the method.
Example 2:
package com.javaprogramto.arrays.returns; import java.util.Arrays; public class ArrayReturnExample2 { public static void main(String[] args) { double[] first5Numbers = getDoublePRimitive(5); System.out.println("Double primitive Array returned from method : " + Arrays.toString(first5Numbers)); Double[] first6Numbers = getDoubleWrapper(5); System.out.println("Double wrapper Array returned from method : " + Arrays.toString(first6Numbers)); } // returning double primitive array from method public static double[] getDoublePRimitive(int size) { double[] numbers = new double[size]; for (int i = 0; i < size; i++) { numbers[i] = i + 1; } return numbers; } // returning Double wrapper array from method public static Double[] getDoubleWrapper(int size) { Double[] numbers = new Double[size]; for (int i = 0; i < size; i++) { numbers[i] = Double.valueOf(i + 1); } return numbers; } }
Output:
Double primitive Array returned from method : [1.0, 2.0, 3.0, 4.0, 5.0] Double wrapper Array returned from method : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
4. Java Return Array - Custom Objects
Example 3:
package com.javaprogramto.arrays.returns; import java.util.Arrays; public class ArrayReturnExample3 { public static void main(String[] args) { CustomObject[] objects = getObjects(); System.out.println("Returned custom objects - " + Arrays.toString(objects)); } // returning custom objects array from method public static CustomObject[] getObjects() { return new CustomObject[] { new CustomObject("Ram", 30), new CustomObject("Sita", 20) }; } } class CustomObject { public String name; public int age; public CustomObject(String name, int age) { this.age = age; this.name = name; } @Override public String toString() { return "CustomObject [name=" + name + ", age=" + age + "]"; } }
Output:
Returned custom objects - [CustomObject [name=Ram, age=30], CustomObject [name=Sita, age=20]]
5. Java Return Array - Multidimensional Array
Example 4:
package com.javaprogramto.arrays.returns; import java.util.Arrays; public class ArrayReturnExample4 { public static void main(String[] args) { int[][] multiArray = getMultiArray(); System.out.println("Returned Multi dimentional array values : "); for(int[] a : multiArray) { System.out.println( Arrays.toString(a)); } } // returning multi dimens public static int[][] getMultiArray() { int[][] multi = { { 1, 2, 3 }, { 4, 5, 6 } }; return multi; } }
Output:
Returned Multi dimentional array values : [1, 2, 3] [4, 5, 6]
6. Conclusion
In this article, we've seen how to return different types of arrays from a method in java.
No comments:
Post a Comment
Please do not add any spam links in the comments section.