1

So, this is my dilemma. I have a class called:

public petShop()

and I used an ArrayList to collect all the strings (names of pets).

ArrayList<String> petNames = new ArrayList<>();

However, when using a different signature with an array in, the return type gives an error.

Any help?

public String[] getPetNames() {
return petNames;
1
  • Are there a fixed amount of names that you can accept in the controller? Commented Dec 3, 2015 at 0:27

1 Answer 1

4

You can get back an array from your ArrayList simply, using toArray.

public String [] getPetNameAsStringArray() {
    return petNames.toArray(new String[petNames.size()]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.