Solved

How to convert a Map into an Array?

Posted on 2004-04-23
12
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

632 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