• Register
1 vote
12 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

Please log in or register to answer this question.

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 17 views
17 views
Can any one from kodlogs show me how to set banckground image in java swing applying eclipse?
asked Sep 26, 2020 Daniel Anderson 4k points
0 votes
1 answer 8 views
8 views
I was working on that but I got into some problems.
asked Dec 3, 2020 TeamScript 9.9k points
0 votes
1 answer 4 views
4 views
Can anyone please explain what is wrong with this and what I need to do to fix it?
asked Dec 19, 2020 TeamScript 9.9k points
0 votes
1 answer 11 views
11 views
Explain with an example?
asked Nov 23, 2020 jagruthi 680 points
0 votes
1 answer 11 views
11 views
Illustrate with an example
asked Nov 16, 2020 jagruthi 680 points
0 votes
1 answer 105 views
105 views
Problem: Hi dear friends is there any way to find out the sum of total element of array which is equal to given number. Please suggest any example.
asked Nov 6, 2020 Mubashi-Question 1.7k points
0 votes
1 answer 21 views
21 views
Problem: Hi! I want to check whether an array is empty or null in Java. Any guidance would be appreciated.
asked Oct 12, 2020 Code Learner 5.5k points
0 votes
1 answer 35 views
35 views
I want to know how to sort an array in java without using sort method.
asked Sep 28, 2020 Daniel Anderson 4k points
1 vote
1 answer 21 views
21 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, 2020 Gavin 15.3k points