Is it possible to add all the elements in a HashSet into an array? Below is a fragment of code I wrote. It tries to transfer all the elements from a HashSet to an array. But when I compile, an error prompts up.
"friends" is a HashSet containing some names and their associated contact details.
"friend" is defined as an object variable of Person type.
My code is:
Person[] contactArray = new Person[friends.size()];
for(int index = 0; index < friends.size(); index++)
{
contactArray[index] = new Person(friend);
}
The problem is with the last statement. Besides adding an element by providing details directly, like this:
contactArray[index] = new Person("John Smith", "0213333333");
is it possible to add a large number of elements with a loop? Can anybody tell me that? Thanks a lot.