• Register
1 vote
9 views

Problem:

Hello programmers, I just want to know how can I return an array in Java programming language?

Thanks in advance.

12 7 7
15,250 points

1 Answer

1 vote
 
Best answer

Solution:

A manual method/function can easily return an array. Make sure the return type of that function is declared as an array of the correct data type. like this

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[] myArray={1,2,3,4,5};  //initializing array  
  return myArray;

  }  
}

This program will print the values of myArray which is 1,2,3,4,5. 
Thanks

13 9 6
94,240 points

Related questions

0 votes
1 answer 4 views
4 views
Can any one from kodlogs show me how to set banckground image in java swing applying eclipse?
asked Sep 26 Daniel Anderson 4k points
0 votes
1 answer 11 views
11 views
Problem: Hi! I want to check whether an array is empty or null in Java. Any guidance would be appreciated.
asked Oct 12 Sheeza 2.6k points
0 votes
1 answer 12 views
12 views
I want to know how to sort an array in java without using sort method.
asked Sep 28 Daniel Anderson 4k points
1 vote
1 answer 12 views
12 views
Problem: Hello Kodlogs, I have got a question from my fellow (java learner) friend and the question is: find the largest number in an array java I know about the array and I can print all the values sequentially. Moreover, I can add two different values of an array in ... Could any of you please give me a proper solution and tell me how to find the largest number in an array java? Thanks to all.
asked Mar 29 Gavin 15.3k points