Equivalent of std::vector in Java?

后端 未结 9 1160
名媛妹妹
名媛妹妹 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:31

    i think it is the LinkedList

    vector (c++)   <===========> linkedlist(java) 
    v.front()      <===========> l.peekFirst() 
    v.back()       <===========> l.peekLast()  
    v.push_back(x) <===========> l.add(x) 
    v.pop_back()   <===========> l.pollLast() 
    
    0 讨论(0)
  • 2020-12-15 03:34

    You're probably looking for the ArrayDeque which supports push/pop style access from both ends of the list efficiently.

    Avoid Stack and Vector - these are synchronized, which implies generally pointless overhead.

    ArrayList is also fine; however, you'd need to implement your own (trivial) pop method since it is not provided by the class itself. ArrayList does permit indexed access, which ArrayDeque lacks.

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

    You can use an ArrayDeque, it doesn't support random access but support Deque (double ended queue) methods

    0 讨论(0)
  • Java has Stack which supports push and pop. (http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)

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

    How about simply the Vector class?

    http://download-llnw.oracle.com/javase/6/docs/api/java/util/Vector.html

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

    That would probably be ArrayDeque, if you need Stack functionality.

    Do not use the Stack class as other here suggest.

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