Link to home
Start Free TrialLog in
Avatar of dhar116
dhar116

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
then to call use:

String[] array = myobject.mymethod();

where myobject is an instance of the class you declared mymethod in
Avatar of dhar116
dhar116

ASKER

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?
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)
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]);
}
youy can also use the following to output the complete array:

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