Link to home
Start Free TrialLog in
Avatar of AquaZubz
AquaZubz

asked on

How to convert a Map into an Array?

i have genereated a hashmap 'm' and converted it into a collection by  using  iterator.

Iterator keysOfTree = m.keySet().iterator();

now i want to put these values in an array , which i am doing this way

            int len = m.size();
            TreeNode[] nodes = new TreeNode[len];                 // Array storing the nodes of this tree
     
            int num1=0;
            while ((keysOfTree.hasNext()) && (num1 < nodes.length)){
                nodes[num1] = (TreeNode)keysOfTree.next();
                num1++;
            }

TreeNode is a class provided in the package and i have declared an array of this type. in this array i want to put the values from the map..
where am i wrong?  

later on, in the code when i am accessing the values of nodes[] array i get Null... y is tht?

plz help me asap!!
Avatar of kuroji
kuroji
Flag of Australia image

You're out by one in the line
 nodes[num1] = (TreeNode)keysOfTree.next();

dereference current element and assign to nodes[num1]
nodes[len] == null
due to this error
Avatar of AquaZubz
AquaZubz

ASKER

kuroji,

i didn't get your point... plz elaborate a lil bit.. i'll be thankful ..

plz help, i mean whts the way out..?

Avatar of Mayank S
I think that there could also be some other problem. Are you sure that you have not messed up something like having a class-member TreeNode array with the name nodes and also a local one?

Also - make sure that control is going inside the while loop. Give some System.out.println () statements to debug.
>> nodes[len] == null

I don't know what you meant by that but when num1 is equal to nodes.length, control will not go inside the while loop only. So the ArrayIndexOutOfBoundsException is saved.
What kuroji means to say is that you might have messed up with the scope of the array "nodes". Make sure that "nodes" is still in scope while you are trying to access it. Could you please show more of the code where you are trying to print the values and you are getting errrors.
>> with the scope of the array "nodes".

That is what I meant ;-)

>> a class-member TreeNode array with the name nodes and also a local one
8-{ dang.
I was out by one this time. nodes[len-1] == null

Which hashmap implementation are you using ?
To answer your question I specifically need to SEE the implementation of Iterator in order to determine whether I have correctly diagnosed the problem.

I suspect keysOfTree.next()  gives you the _next_ node, not the current one !
I just figured out you're using Java. <heavy sigh>
The Iterator concept in Java is seriously flawed.
(the remove method is not safe to call until 'after' calling the next method)

My suspicions were incorrect, it appears you are using Iterator correctly.

In which case you will need to show more code if we're to help you.
Since this is Java and I don't pollute my language related brain space with that rubbish, I am opting out of this thread, byey.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please proceed with that recommendation.