java中的Push_back和pop_back

nik*_*hil 0 c++ java stl vector arraylist

我来自C++背景.我使用向量push_back和pop_back方法来推送和弹出向量中的元素.我知道arraylist等同于vector,但我没有在arraylist API中找到与push_back和pop_back等效的方法.我能找到的壁橱是LinkedList.我错过了什么或者在C++中还有其他等价的向量吗?

任何帮助表示赞赏.

San*_*ela 6

使用add()ArrayListpush_back.

因为pop_back你必须使用索引来玩更多remove().

我们来看看这个例子:

// push_back equivalent
ArrayList<int> a = new ArrayList<int>();
a.add(2);             // Add element to the ArrayList.
a.add(4);

// pop_back equivalent.
a.remove(a.size()-1); // Remove the last element from the ArrayList.
Run Code Online (Sandbox Code Playgroud)

  • 'pop_back'等价物可以更直接:"a.remove(a.size() - 1);".没有必要先"搞定"它. (2认同)