21

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?


enter image description here

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
  • 1
    Vectors aren't the same as ArrayLists, namely because of built-in synchronization. More info here: stackoverflow.com/questions/2986296/… Commented Aug 5, 2013 at 22:33
  • Off-topic: The term "vector" is not just used in Java. 1. The C++ library has std::vector<…> (dynamically-sized "arrays"). 2. The CLR (.NET runtime) also distinguishes between "arrays" and "vectors", the latter being a special case of the former, namely one-dimensional, zero-based ones). That distinction is important because there are special optimizations for vectors. 3. In mathematics, a n-tuple of real numbers is also sometimes called "vector".
    – stakx
    Commented Aug 5, 2013 at 22:49
  • 1
    @JeroenVannevel I think you made the confusion between the Vector class and the concept of vector which can be considered as a unidimensional array. Commented Aug 5, 2013 at 23:08
  • OT, if you want answer from computer science people, why post here and not at cs.stackexchange? And why the java flag? From what I remember from my programming class, there's array and then there's linked-list. Except for array, containers (vector, list, map, heap, etc.) are implemented as linked-list.
    – imel96
    Commented Aug 5, 2013 at 23:48
  • 2
    Why do we call a vector an array?
    – Random42
    Commented Aug 6, 2013 at 6:55

8 Answers 8

37

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
  • 10
    Off-topic: In mathematics, is a "scalar" a 0-dimensional array?
    – stakx
    Commented Aug 5, 2013 at 22:52
  • 8
    @stakx: Yes, that's exactly right. Usually a scalar is just a scalar, nobody would actually call it a "0-dimensional array". But the idea is right. Commented Aug 5, 2013 at 22:58
  • 3
    It is more accurate to say they are all tensors. From Wolfram Mathworld: "Tensors are generalizations of scalars (that have no indices), vectors (that have exactly one index), and matrices (that have exactly two indices) to an arbitrary number of indices." (Rowland, Todd and Weisstein, Eric W. "Tensor." From MathWorld--A Wolfram Web Resource. mathworld.wolfram.com/Tensor.html)
    – SOFe
    Commented Feb 27, 2018 at 11:36
  • 1
    For what is worth, in mathematics a vector is not a 1-dimensional array. A vector is an element of a vector space, that is, a set in which elements can be added and multiplied by scalars.
    – mlainz
    Commented Apr 9, 2019 at 9:41
  • The most famous vector space is R^n, the set of "arrays" of numbers with element-wise addition and multiplication. The set of real functions is also a vector space, but usually people don't think of functions as arrays. An array of characters wouldn't be a vector space, since there is no obvious choice of addition and multiplication. In mathematics jargon, an array is a tuple or a finite sequence. See en.wikipedia.org/wiki/Vector_(mathematics_and_physics) for more examples of tuples that are not vectors.
    – mlainz
    Commented Apr 9, 2019 at 9:50
11

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
  • 4
    I think you made the confusion between the Vector class and the concept of vector which can be considered as a unidimensional array and ils note specific toi Java. Commented Aug 5, 2013 at 23:09
  • @C.Champagne He used a Java example so that's what I gave him. I've added the definition for the mathematical construct, which incidentally isn't technically a "vector" either.
    – user28988
    Commented Aug 5, 2013 at 23:11
  • 3
    I'm with @C.Champagne. If they meant Vector (the class) the text would have used a cap V. They used a lowercase v which indicates the classical (not the class) definition of a vector.
    – rlperez
    Commented Aug 6, 2013 at 4:03
  • Um, vectors are called vectors in math. What do you mean, "not technically a 'vector' either"?
    – Andres F.
    Commented Aug 7, 2013 at 21:14
  • @AndresF. A set of numbers like an array or a "Vector" would be an n-tuple is my understanding because it is a discrete collection of values.
    – user28988
    Commented Aug 7, 2013 at 21:20
6

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 3x3 matrix can be represented by a "2-dimensional" array, which is what programmers call an array of arrays. Yet a 3x3 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
  • I don't think this is true. In almost all documentation I've run across Array implies fixed size, Vector implies the structure can be (but not necessarily is) dynamically sized. The mathematical definitions of the terms are incredibly misleading in the context of programming.
    – Chuu
    Commented May 30, 2014 at 21:18
  • @Chuu I don't think you fully understood this answer which is talking about very general perspective of vector and array; not something in C++.
    – firo
    Commented May 16, 2020 at 12:35
-1

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.

-1

I think the term "vector" came from C++'s std::vector. It appeared before Java and C#.

1
  • 3
    Please (strongly) consider adding an external reference to back up your opinion.
    – user53019
    Commented Aug 7, 2013 at 3:39
-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
  • 1
    "Early versions ... had"? And what is java.util.Vector? Commented Aug 8, 2013 at 13:52
  • 1
    Deprecated is what java.util.Vector is. :-) Commented Aug 8, 2013 at 19:02
-1

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.

-2

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.