3

In C++ i can insert an item into an arbitrary position in a vector, just like the code below:

std::vector<int> vec(10);
vec.insert(vec.begin()+2,2);
vec.insert(vec.begin()+4,3);

In Java i can not do the same, i get an exception java.lang.ArrayIndexOutOfBoundsException, code below:

Vector l5 = new Vector(10);
l5.add(0, 1);
l5.add(1, "Test");
l5.add(3, "test");

It means that C++ is better designed or is just a Java design decision ? Why java use this approach ?

  • 1
    `Vector` is depricated in java, use `ArrayList` instead – bsiamionau Feb 27 '13 at 19:46
  • 1
    Or maybe C++ is worse-designed. – Dave Newton Feb 27 '13 at 19:46
  • 8
    @DaveNewton take that back haha – dchhetri Feb 27 '13 at 19:47
  • 1
    It just means you haven't read the java docs carefully. – juanchopanza Feb 27 '13 at 19:52
  • @user814628: Dave is correct up to some level. In Java it is safely designed, since it is a safe high level language than C++. But I disagree with "worse-designed" statement, since Java derives from C++ and it widely follows C++ technologies. Anyway this question is all about techniques used in data structures. – PeakGen Feb 27 '13 at 20:19
  • another thing, someone has down voted this question, and I up voted it. Dear developers, there is no need of down voting questions :) – PeakGen Feb 27 '13 at 20:21
  • Where is Vector in Java deprecated? Not even in Java 10. It is just recommended for different purposes. Vector is thread-safe, ArrayList not. – Hauke Sep 19 '18 at 08:27

3 Answers3

8

In the C++ code:

std::vector<int> vec(10);

You are creating a vector of size 10. So all indexes from 0 to 9 are valid afterwards.

In the Java code:

Vector l5 = new Vector(10);

You are creating an empty vector with an initial capacity of 10. It means the underlying array is of size 10 but the vector itself has the size 0.

It does not mean one is better designed than the other. The API is just different and this is not a difference that makes one better than the other.

Note that in Java it is now preffered to use ArrayList, which has almost the same API, but is not synchronized. If you want to find a bad design decision in Java's Vector, then this synchronization on every operation was probably one.

Therefore the best way to write an equivalent of the C++ initialization code in Java is :

List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++){
    list.add(new Integer());
}
Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • so the latter is akin to `vec.reserve(10);` – 111111 Feb 27 '13 at 19:47
  • 3
    In Java, you shouldn't use `Vector`, please refer to [Why is Java Vector class considered obsolete or deprecated?](http://stackoverflow.com/q/1386275/1065197). Instead, please recommend using `List` backed up with `ArrayList`. – Luiggi Mendoza Feb 27 '13 at 19:51
  • @LuiggiMendoza: I have seen people working with Java 1.4 and they need vector. But I agree with you 200% :) – PeakGen Feb 27 '13 at 20:23
  • @Yohan I've seend people using Vector in Java 1.4 but I still don't understand the main reason. If it was for *synchronized work*, well, they had started with a wrong design and didn't wanted to start making the changes to a better approach/design. – Luiggi Mendoza Feb 27 '13 at 20:28
  • @LuiggiMendoza: Yeah, agreed. But there are 2 main reasons. First one is outdated environment. Some work with old web containers with J2EE. J2EE is 100% OK, but they do not use the updated versions of J2EE (They dnt have to use JEE, they dnt use J2EE new versions). Second reason is people don't like to change. Some companies are there who work with Visual Studio 2000 LOL! So we have to expect the same for Java from them. These 2 are my personal experience. – PeakGen Feb 27 '13 at 20:41
2

The Javadoc for Vector.add(int, Object) pretty clearly states that an IndexOutOfBoundsException will be thrown if the index is less than zero or greater than or equal to the size. The Vector type grows as needed, and the constructor you've used sets the initial capacity, which is different than the size. Please read the linked Javadoc to better understand how to use the Vector type. Also, we java developers typically use a List type, such as ArrayList in situations where you would generally use a std::vector in C++.

1

Differences? You cannot compare how 2 languages do those. Normally Vector do use Stack data structure or LinkedList (or may be both). Which means, you put one item to the top, put another item on top of it, another item even on top of it, like wise. In LinkedList, it is bit different, you "pull" the value but the same thing. So in C++ it is better to use push_back() method.

C++ Vector objects are instantiated automatically. But in Java it is not, you have to fill it. I disagree with the way of filling it using l5.add(1, "Test");. Use l5.add("test").

Since you asked differences, you can define your object in this way as well

 Vector a = new Vector();

That is without a type, in Java we call it without Generics. Possible since Java 1.6

Vector is now not widely used in Java. It has delays. We now move with ArrayList which is inside List interface.

Edit

variable names such as l5 are widely used in C++. But Java community expects more meaningful variable names :)

PeakGen
  • 21,894
  • 86
  • 261
  • 463