Solved

How to convert a Map into an Array?

Posted on 2004-04-23
12
319 Views
Last Modified: 2010-04-17
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!!
0
Comment
Question by:AquaZubz
12 Comments
 
LVL 3

Expert Comment

by:kuroji
ID: 10907936
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
0
 

Author Comment

by:AquaZubz
ID: 10910770
kuroji,

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

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

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10915979
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.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10915998
>> 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.
0
 
LVL 3

Expert Comment

by:PerlKing
ID: 10916874
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10916968
>> 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
0
 
LVL 3

Expert Comment

by:kuroji
ID: 10934428
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 !
0
 
LVL 3

Expert Comment

by:kuroji
ID: 10934515
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.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 25 total points
ID: 10936371
>> I don't pollute my language related brain space with that rubbish

As a matter of fact, it stays as the only thing which is portable across any platform and more reusable.

>> The Iterator concept in Java is seriously flawed.

No.

>> the remove method is not safe to call until 'after' calling the next method

That's because of cursor positions. Logically, you can imagine that initially, the cursor is at -1. Now you have to check if there are more items or not. So you do itr.hasNext () and if it gives you true, then you are at a valid item position, and then you can get the object using next () and perform what-ever operations you want to do with that object. In fact, its a similar concept to what you have in cursors in SQL too - don't call that flawed as well.

In fact, Iterator is just an interface in Java. Depending upon the kind of Collection that it is iterating, it returns back to you an appropriate object which implements Iterator and then you can use that Iterator for iterating that Collection. I find the concept rather clean.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11162348
Please proceed with that recommendation.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
control image tags in a string ? 12 130
Base1 Encode/Decode 3 77
This code tracks birthdays 3 73
Definitions and default visual studio colors 5 65
A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question