-5

I used to write c++ like this vectorV[size] or vector> but I don't know how to write it in java. I tried to write like this Vector> x = new Vector>(size) but when I want to use it like this x.get(index).add(new object) ,it will have an ArrayIndexOutOfBoundsException .How can I solve it ,help me plz ,thx.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
lxhsb
  • 3
  • 1
  • 1
    Please show more of your non-working code. It would help if we could fully understand what you might be doing wrong. – Hovercraft Full Of Eels Mar 13 '16 at 12:59
  • You *probably* don't want a `Vector` because it is broken and garbage. Use an `ArrayList` – Idos Mar 13 '16 at 13:06
  • @Idos Would you mind explaining what is "broken and garbage" about a `Vector`? – Erwin Bolwidt Mar 13 '16 at 13:19
  • 1
    Sure, http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Idos Mar 13 '16 at 13:20
  • @Idos The accepted answer is that you shouldn't use a Vector unless you want to have a synchronized implementation of List. Which is correct. And is something **completely** different than "broken and garbage". Btw you may want to respond to people by prefixing your comment with @ and their name, otherwise they don't see your response. – Erwin Bolwidt Mar 13 '16 at 14:15
  • You can Google for further information, but the fact is that you **almost never** want to use `Vector`, and that is what I meant. We can argue over terminology but that doesn't really matter. – Idos Mar 13 '16 at 14:17
  • It is the first time that I use Stackoverflow and I really apprecitate that your help! – lxhsb Mar 16 '16 at 00:45

2 Answers2

1

The point is that you are able to allocate the C++ vector to initially contain size instances, where those elements are empty vectors.

auto vectorOfVectors = vector<vector<T>>(10);
cout << vectorOfVectors.size() << endl;  // Outputs 10.

You don't the same thing with Java Vector. Even if you construct it with a capacity parameter, all you are doing is asking for space for that many elements to be reserved in the vector. The vector's size is initially zero (unless you call the Vector(Collection<? extends E>) constructor with a non-empty collection as the parameter).

Vector<Vector<T>> vectorOfVectors = new Vector<>(10);
System.out.println(vectorOfVectors.size());  // Outputs 0.

If you want to allocate a vector with size elements, you need to do it explicitly:

Vector<Vector<T>> vectorOfVectors = new Vector<>(size);
for (int i = 0; i < size; ++i) {
  vectorOfVectors.add(new Vector<T>());
}

However, Vector is a strongly deprecated class. You probably want to be using ArrayList instead (in exactly the same way).

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

If I get you right, you need nested vector. You can use nested ArrayList in Java instead of that.

ArrayList<ArrayList<Object>> arrayList = new ArrayList<ArrayList<Object>>();
ArrayList<ArrayList<Object>> al1 = new ArrayList<ArrayList<Object>>();
ArrayList<ArrayList<Object>> al2 = new ArrayList<ArrayList<Object>>();
arrayList.add(al1);
arrayList.add(al2);
arrayList.get(0).add(new Object()); // retrieves al1

Tell me if you need clarification.

Sida
  • 160
  • 8