1

I'm using Java programming language.

C++ has vector<T> and I need the equivalent vector in Java.

I want to convert this code to Java.

Vector<T> a[Maxn]; // Example: string, int, myclass, myvar, ...
int n;
cin >> n;
for(int i=0; i<n; i++)
{
    T x, y;
    cin >> x >> y;
    x--, y--;
    v[x].push_back(y);
}
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489

6 Answers6

3

You are probably looking for ArrayList

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

Something like this:

ArrayList ar = new ArrayList<String>();
ar.add("abc");
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

You can use List<T>, Java has many better ways, take it easy!

you could Search it...

1

Have a look at java.util.List. There are many concrete implementations of list including java.util.ArrayList

Here is an example using ArrayList Note Java Collections e.g. List<T> make use of generics. Below I am using a list of String

List<String> list = new ArrayList<String>(); 
list.add("some string");
cmd
  • 11,622
  • 7
  • 51
  • 61
0

I'm pretty sure that this is the class you are looking for:

java.util.List

Charles Forsythe
  • 1,831
  • 11
  • 12
0

If you want something analogous to C++'s std::vector, I would start by looking at the various classes that implement the List interface (http://docs.oracle.com/javase/7/docs/api/java/util/List.html).

Probably the most commonly-used List is ArrayList, which has all the normal operations you would expect - add, get, size, iterator, etc.

Alternatively there is LinkedList, which is useful in its own way, depending on what exactly you're trying to achieve.

CmdrMoozy
  • 3,870
  • 3
  • 19
  • 31
-2

EDIT: I do not advocate using java.util.Vector<E>, but since you are coming from a C background, it might give you a warm fuzzy to use the same name. However, you should note (from the Java API)

Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.

So it's best to use some other implementation of java.util.List<E> -- most common to use java.util.ArrayList<E>

MadConan
  • 3,749
  • 1
  • 16
  • 27
  • So why use Vector over the *newer* ArrayList? (C++'s vector has no such synchronization guarantees.) – user2864740 Oct 30 '13 at 17:41
  • Only use Vector if you *need* sychronization (thread safety). If the container won't be accessed from multiple threads, there's no need to incur the overhead. – MadConan Oct 30 '13 at 17:42
  • Hmmm. Now down voting correct answers. Sweet. – MadConan Oct 30 '13 at 17:43
  • 2
    Read this: [Why is Java Vector class considered obsolete or deprecated?](http://stackoverflow.com/q/1386275/1065197) – Luiggi Mendoza Oct 30 '13 at 17:43
  • I'm not advocating the use of Vector. Just that it IS available. editing my answer... – MadConan Oct 30 '13 at 17:44
  • 1
    Still you're promoting to use something that you should not. – Luiggi Mendoza Oct 30 '13 at 17:45
  • 1
    I disagree that I'm "promoting it." I actually don't recommend it. – MadConan Oct 30 '13 at 17:48
  • Since 1) it's obsolete/deprecated, and 2) nothing in the OP's post indicated that s/he expected any sort of synchronization, I'd argue that it isn't worth mentioning. – CmdrMoozy Oct 30 '13 at 17:50
  • @CmdrMoozy `Stack` extends from `Vector`. You should update your answer as well... – Luiggi Mendoza Oct 30 '13 at 17:52
  • Obsolete? Sure. Deprecated? Not seeing that in the API. And I think it is worth mentioning, at least in a dark manner, since there's so much code out there using it. – MadConan Oct 30 '13 at 17:52
  • @LuiggiMendoza You're right - I didn't look closely enough at the docs before adding it to my answer. – CmdrMoozy Oct 30 '13 at 17:54
  • It is worth to mentioning as *you just at least know of the existence of this class, but **do not** use it*. Before your edits, you were promoting the usage of this class, that's why I downvoted it. – Luiggi Mendoza Oct 30 '13 at 17:54
  • @CmdrMoozy anyway, for both of you, there's no meaning to answer a duplicated question to begin with... – Luiggi Mendoza Oct 30 '13 at 17:55