944,165 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7800
  • Java RSS
Apr 11th, 2005
0

Is ArrayList Better than Vector

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grifflyn is offline Offline
17 posts
since Sep 2004
Apr 11th, 2005
0

Re: Is ArrayList Better than Vector

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 11th, 2005
0

Re: Is ArrayList Better than Vector

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 12th, 2005
0

Re: Is ArrayList Better than Vector

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Have a ")" problem I can't solve
Next Thread in Java Forum Timeline: Method help that calculates a function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC