Equivalent of std::vector in Java?

后端 未结 9 1161
名媛妹妹
名媛妹妹 2020-12-15 03:16

What would be the closest thing to a std::vector in Java? By this I mean, a class which can take in T into its constructor and then pushBack, popBack() and that is stored in

相关标签:
9条回答
  • 2020-12-15 03:50

    ArrayList
    Everything's stored in array ("continuous memory") internally, although operation names are a bit different.

    A bit more about list implementations in Java
    And about generics

    edit
    Helper Method also mentioned useful class in his answer (although not exactly equivalent to C++ Vector).

    0 讨论(0)
  • 2020-12-15 03:50

    What you need is exactly an java.util.ArrayList<T> You can check the documentation in http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

    Basically is a List implemented with an Array where the references live in a continuous chunk of memory.

    I recommend to use in combination with a interface variable like this: List<String> stringList = new ArrayList<String>(); so if you decide, you can change the implementation to java.util.LinkedList<T> or another one.

    0 讨论(0)
  • 2020-12-15 03:51

    Is ArrayList what you're looking for?
    ArrayList l = new ArrayList<String>();
    So you can have a list of anything (defined between the <>).

    0 讨论(0)
提交回复
热议问题