Solved

How to convert a Map into an Array?

Posted on 2004-04-23
12
318 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
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…

910 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

17 Experts available now in Live!

Get 1:1 Help Now