I am reading a book on Java Programming, and want to confirm I understand the definition of the word “vector”. Wikipedia says vector is “A one-dimensional array”, source http://en.wikipedia.org/wiki/Vector.
Wouldnt it be simpler to call the array simply an array? Is there any reason we need to use such fancy language as “vector”? Is there a difference between an array and a vector?
Source: Cracking the Coding Interview, 4th Edition, by Gayle McDowell, page 47.
FAQ
Q – Why didnt you post this on english.stackexchange.com?
A – Because I think only computer science oriented people will have a good answer.
6
In typical usage, an “array” can mean either a single-dimensional array, or a multidimensional array. Also, in mathematics, a matrix is a 2-dimensional array while a vector is a 1-dimensional array.
5
Vectors aren’t exactly arrays. Not classical ones anyway. They are dynamic arrays. They can be resized as needed rather than being of a fixed size. They are thus similar to ArrayLists but not quite the same.
ArrayLists differ from Vectors in that Vectors synchronize individual operations which is something you don’t want for concurrency purposes in particular per Jon Skeet. Thus Vector has been deprecated in favor of ArrayList but some still call it a Vector.
The name is originally derived from the mathematical term for a 1 dimensional matrix. This is in spite of the structure actually being an n-tuple.
8
Usually, vector and array are the same thing and are used interchangeably when programming. That is in most places, so you shouldn’t worry much about it most of the time.
That said, language is imprecise and we sometimes have words that have different meanings in different contexts that end up meaning the same thing, or one word having two or more different meanings.
In C an array is a series of data elements of the same type that are stored in memory contiguously. Java inherited that meaning. It’s not the same as a List or a Vector, as arrays are more basic than those.
A vector is a mathematical construct defined as an object that can be added to another object of the same type, or be multiplied by any real (or complex) number resulting in another vector of the same type. It also has a few other properties that are very useful.
A vector has dimension. It’s the minimum number of different vectors that have to be combined to get every possible vector of that type. Velocity and acceleration are 3-dimensional vectors because space has 3 base directions and by adding multiples of those three base directions you can get any position in space. Position in a plane is a 2-dimensional vector, and individual numbers are 1-dimensional vectors.
One way to represent a vector of dimension n is by using an array of n elements each one representing how much of each base vector you have to add to get to your vector.
Since you can represent a vector using an arra of elements, with time, the two concepts were equated. So, in many places, they simply are the same thing and in some languages arrays are called vectors.
Another case where one word has two different meanings is, for instance, dimension. The wikipedia definition of a vector as a 1-dimensional array is an example. You are not talking of vector dimension here. You are talking about the computer representation of the data structure. A 3-Dimensional vector can be represented by a “1-dimensional” array of size 3. 3 numbers in line. A 3×3 matrix can be represented by a “2-dimensional” array, which is what programmers call an array of arrays. Yet a 3×3 matrix is also a mathematical vector of dimension 9 (since it has all the properties of a vector) and has 9 numbers. Confusing, huh?
Anyway, I think the answer is simply: don’t worry. It all depends on context. The two words have different origins, but in the context of data structures, when they say vector they mean exactly array.
2
The answers above describe why this class is different from an “Array” – and I suspect the reason a different name is used is because programmers are helped by having a well organised namespace – in other words if you talk about a “Vector” it is clear precisely what class you mean, while if all similar classes were merely arrays then it would not be fully clear.
I think the term “vector” came from C++’s std::vector. It appeared before Java and C#.
1
It’s just sloppy editing.
Early versions of Java did not include ArrayList. Instead of ArrayList, Vector was used. ArrayList was added later and is now used instead of Vector for most purposes. I expect that the book was written before ArrayList was added, and that when the text was updated, this reference to vector was not.
I suggest you avoid this sort of “learn it all in ten minutes” book. Generally they are written quickly and sloppily. This level of knowledge won’t get you a job in a serious shop.
Instead seek out well-reviewed books by recognized users of the technology covered and read for understanding.
2
The term vector comes from engineering/physics. Vectors represent 2 and 3 dimensional lines that have a direction. For example, let’s say a projectile has horizontal velocity of 20 m/s and a vertical velocity of 10 m/s. So you would then represent this as (20,10). It’s flying at kind of a diagonal right, so now see how mathematically velocity vectors become arrays aka vectors.
We don’t call arrays vectors, either you misunderstood your source or the source misunderstood Java, or both.
An array is a quite different datastructure from a Vector, which is different from a List again (which your pasted code uses).
Of course a mathematical vector could in Java be implemented using either of the three mechanisms, and under the hood either or both the Vector and List might be implemented using arrays.