• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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