person
person

Reputation: 13

A method uses an array and returns an array, but I am using an array list

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;

Upvotes: 1

Views: 62

Answers (1)

jor
jor

Reputation: 158

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

public String [] getPetNameAsStringArray() {
    return petNames.toArray(new String[petNames.size()]);
}

Upvotes: 4

Related Questions