Reputation: 13
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
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