Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: about adding elements for an array  (Read 4220 times)

0 Members and 1 Guest are viewing this topic.

nnf2540

  • Guest
about adding elements for an array
« on: September 25, 2006, 01:01:58 AM »
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.
 ;D