Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

Declaring a method which returns string array and call this method else where

I am fairly new to java and I need to declare a method which should basically return a string array and needs I need to call this function later to use these elements.

1. I need to know how to declare this method and store some values into array
2. I need to call this method in another method to get these values and use them

0
dhar116
Asked:
dhar116
  • 5
1 Solution
 
objectsCommented:
to declare method use something like:

public String[] mymethod()
{
   String[] result = new String[3];
   result[0] = "fdfd";
   result[1] = "fdfd";
   result[2] = "fdfd";
   return result;
}
0
 
objectsCommented:
then to call use:

String[] array = myobject.mymethod();

where myobject is an instance of the class you declared mymethod in
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
dhar116Author Commented:
Thanks, I have lot of elements in the array. When i call the array, I see only the last element. I cannot see the rest. How do i see all if I want to?
0
 
objectsCommented:
you can access any element in the array using its index

eg. to get the 10th value use:

String[] array = myobject.mymethod();
String s10 = array[9];

(NB. Java array indexes are 0-based)
0
 
ThangamanirCommented:
Or you can use the for loop to access all the array elements

for(i=0;i<array.length();i++)
{
  System.out.println(array[i]);
}
0
 
objectsCommented:
youy can also use the following to output the complete array:

System.out.println(Arrays.asList(array));
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now