Solved

How to convert a Map into an Array?

Posted on 2004-04-23
12
317 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
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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:
mayankeagle earned 25 total points
Comment Utility
>> 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:mayankeagle
Comment Utility
Please proceed with that recommendation.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now