• Register
1 vote
15 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 3 views
3 views
Problem: How to set background image in java swing jpanel? Help me.
asked Apr 1 zayed1 35.6k points
0 votes
1 answer 36 views
36 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
1 vote
1 answer 2 views
2 views
Problem: I am unable to find out the solution, Please solve it : how to return the index of an array in java
asked Apr 8 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 9 views
9 views asked Jan 25 ahmedadel9090 3.1k points
1 vote
1 answer 2 views
2 views
Problem: I am unable to find out the solution, Please solve it : how to return an array in javascript
asked Apr 8 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 2 views
2 views
Problem: Can anyone tell me the solution of this problem? How to return an array in c++?
asked Apr 3 Ifra 24.4k points
1 vote
1 answer 3 views
3 views
Problem: Please help me. How can I solve it? can you return an array in c
asked Apr 6 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 556 views
556 views
Problem : I am completely stuck on this problem Given an array of ints, return True if the array contains a 2 next to a 2 somewhere. has22([1, 2, 2]) → True has22([1, 2, 1, 2]) → False has22([2, 1, 2]) → False I know the basic idea but I am unable to implement it. ... this, eg. graph, search? def has22(mynums): for l in mynums: if ( (mynums[l] = 2) and (mynums[l+1] = 2) ) return True return False
asked Dec 10, 2019 alecxe 7.5k points