Is ArrayList Better than Vector

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 17
Reputation: grifflyn is an unknown quantity at this point 
Solved Threads: 0
grifflyn's Avatar
grifflyn grifflyn is offline Offline
Newbie Poster

Is ArrayList Better than Vector

 
0
  #1
Apr 11th, 2005
I have written this bit of code is ArrayList better than Vector and if so how do i change it ????

i am thinking where ever vector is mentioned to change it to ArrayList
  1. import java.util.Vector;
  2. import java.io.*;
  3.  
  4.  
  5. public class V_All_Std_Record
  6. {
  7.  
  8. private Vector main_Vector = new Vector();
  9.  
  10. public V_All_Std_Record()
  11. {
  12. try
  13. {
  14. File std_File = new File("");
  15. String str = std_File.getAbsolutePath()+"\\Data Files\\Std_Rec.txt";
  16. ObjectInputStream obj_File = new ObjectInputStream(new
  17. FileInputStream(new File(str)));
  18.  
  19. main_Vector = (Vector)obj_File.readObject();
  20. obj_File.close();
  21. System.out.println(main_Vector);
  22.  
  23. }
  24. catch(Exception ee)
  25. {
  26. System.out.println("Student Data file not Found or there is no data in it");
  27. }
  28.  
  29.  
  30.  
  31. }
  32. public static void main(String[] args)
  33. {
  34. V_All_Std_Record v_All_Std_Record1 = new V_All_Std_Record();
  35. }
  36.  
  37. //returns all studentd records
  38. public Vector getAllRecords()
  39. {
  40. return main_Vector;
  41. }
  42.  
  43. }
Code tags added. -Narue
:p
If you worry you Die.
If you dont worry you Die.
SO DONT WORRY
:cool:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Is ArrayList Better than Vector

 
0
  #2
Apr 11th, 2005
>is ArrayList better than Vec
It depends on your needs. If you need a synchronized list and it has to be compatible with Java 1.1 then Vector is an option. Otherwise, ArrayList should be preferred, and Collections.synchronizedList will give you synchronization (which ArrayList does not have by default).

>how do i change it ????
Compare the API documentation on Vector and ArrayList. That will show you everything you need to change. You may be surprised though.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Is ArrayList Better than Vector

 
0
  #3
Apr 11th, 2005
I thought the Vector data structure was old, but I could be wrong. If there is a solution that could be done in ArrayList or Vector form, I would probably choose ArrayList.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Is ArrayList Better than Vector

 
0
  #4
Apr 12th, 2005
Vector is indeed old. It is kept around mainly for legacy reasons (backwards compatibility with old code) and its use it not advised in new applications.

Sadly many tutorials and beginners' books were written ages ago and only minimally updated to keep track of new developments and thus still use Vector, thus leading many people who start out in Java to learn to use something they really shouldn't.

I've been wondering for some time now why Sun didn't just deprecate Vector, which would cause compiler warnings to be thrown whenever it's used (thus deterring people away from Vector).

ArrayList is the most common replacement, though other Lists exist (such as LinkedList) for specific scenarios.
It's faster (because it's not synchronized) and fully implements the List interface (something recently backfitted to Vector as well).
Even when needing synchronization, ArrayList (in its synchronized form) should be preferred over Vector.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC