954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Vectors Versus Arrays

I am designing a Rinex file reader class in Java and this is my first project in Java. I need to store data into an array or vector. I am reading the data from the file related to the time the data occurs. At first I tried to use an array but I do not know the size of the array ahead of time. Is there anyway to make an array change size as it grows in a loop? For example in Matlab you could initialize the array to be an array of zeros. Or do I have to use vectors to do this? I also have this problem with a 3D array that I was creating. How would I use vectors to create a 3D array? Thanks!!

kbradley
Newbie Poster
7 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

No, you cannot change the size of an array.
Use an ArrayList instead, not a Vector. Vector has notoriously poor performance, and should only be used when an ArrayList cannot provide the functionality needed while a Vector can (by the time you know when that is you know what it is).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Vectors are evil, look up the ArrayList if you want an array that grows as needed.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks for your help. I have tried looking up arraylist on the Java Api site but I am having a little trouble understanding what it is and what syntax is required to make it work. Can anyone give me a better explanation than what is on Sun's page? Thanks!!

kbradley
Newbie Poster
7 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 
import java.util.*;

class Test
{
     public static void main(String[] args)
     {
            ArrayList al = new ArrayList();
            al.add("one");
            al.add("two");
      }
}


Notice how there's no set size anywhere.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

How can someone understand Vector but not ArrayList?
They have the exact same interface...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Hi everyone,

Vectors are evil, look up the ArrayList if you want an array that grows as needed.

Narue what makes you say that vectors are evil?

Richard West
*****************************************************

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

The same reason I say they should be deprecated (or better yet, banned to the 7th circle of computer hell) :)
They're slow, have a confusing API (with many duplicate functions) because of the way they were retrofitted to support the List interface, are generally not needed (and when you could have a case for them there are still better alternatives), etc. etc.

Basically they're among the dinosaurs of Java, together with StringTokenizer and some other very old classes that now have better alternatives but have been kept on because someone forgot to remove them.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You