Answer» 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.
|