Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to convert a Map into an Array?

Posted on 2004-04-23
12
Medium Priority
?
324 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
10 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 100 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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.
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 …
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…

783 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