General Java Questions I
Kevin T. Smith
 Q: About the order of my elements in hastable...
I save some data into hashtable.
     For example,
hashtable.put ( "1", "one" );
hashtable.put ( "2", "two" );
hashtable.put ("3" , "three" );
when I get back the element from hashtable using Enumeration class ,
Enumeraton e = hashtable.keys();
while ( e.hasMoreElements() ){
   Object k = e.nextElement();
   Object v = hashtable.get (k );
}
the default result is
"3","three"
"2", "two"
"1", "one"
here I want to get the data sorted as ascending. ( the order as I insert ) such as
"1", "one"
"2", "two"
"3", "three"
or is it possible to get the data from end of hashtable?
Answer: When you insert elements into a Hashtable, they will not be stored in the
order you insert them. They are stored in a way that makes it easy to find them by the
key you specify. 
So, you must either use another data structure (e.g. Vector) or sort them after you
retrieve them from the Hashtable (e.g. by using java.util.Collections, java.util.Arrays).
  
Marco Schmidt
 Q: What is better to use: array or vector?
Just wondering as I am using Vectors to store large amounts of objects from 50 to
4000 and each one has to be "looked at" every time paint is called...
Just wondering if it would be better to use an array, list etc?
Answer 1: Since the Vector method uses an array for storage but has extra steps
involved in getting an element, use an array for fastest access.
  
WBB Java Cert mock exams http://www.lanw.com/java/javacert/
Answer 2: arrays are faster, vectors are more dynamic.
This should be evident just looking at the amount of code you need to traverse one
versus the other. It might also be beneficial to write a linkedlist class and use that.
That way you have a dynamic container which has potential to be faster than a vector
(though still not as fast as an array). The problem with arrays is that if you need more
space than the current size, you have to hardcode their copying into a bigger array.
file:///F|/a_jsite/350_tips/general_java I.htm (25 of 33) [2001 07 08 11:24:51]






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved