java java中的push_back和pop_back

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43055216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:03:05  来源:igfitidea点击:

Push_back and pop_back in java

javac++arraylistvectorstl

提问by nikhil

I am coming from a C++ background. I use vector push_back and pop_back methods to push and pop out elements from the vector. I know arraylist is sort of equivalent to vector, but I don'f find equivalent methods to push_back and pop_back in arraylist API. The closet I could find is LinkedList. Am I missing something or is there any other equivalent to vector in C++?

我来自 C++ 背景。我使用向量 push_back 和 pop_back 方法从向量中推送和弹出元素。我知道 arraylist 有点等价于向量,但我没有在 arraylist API 中找到 push_back 和 pop_back 的等效方法。我能找到的壁橱是LinkedList。我是否遗漏了某些东西,或者是否还有其他与 C++ 中的 vector 等效的东西?

Any help is appreciated.

任何帮助表示赞赏。

回答by Santiago Varela

Use add()from ArrayListto push_back.

使用add()ArrayListpush_back

For pop_backyou have to play around a bit more with indexes and remove().

因为pop_back您必须更多地使用索引和remove().

Let's look at this example:

让我们看看这个例子:

// 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.

回答by Shravan40

You can use add()for push_back()and remove()for pop_back().

您可以使用add()forpush_back()remove()for pop_back()

ArrayList<data_type> data = new ArrayList<data_type>();
data.add(value1);
data.add(value2);
data.remove(data.size() - 1);

P.S : arraylistis a container not API.

PS:arraylist是容器而不是API。

回答by HDJEMAI

We can use the LinkedList<E>data structure

我们可以使用LinkedList<E>数据结构

Adding at the end by: add(E e)

在最后添加: add(E e)

removing at the end by using: pollLast()

最后使用以下方法删除: pollLast()

Example:

例子:

package test;

import java.util.*;  
public class test{  
public static void main(String args[]){  

    LinkedList<String> al=new LinkedList<String>();  
    al.add("element1");  
    al.add("element2");  
    al.add("element3");  

    Iterator<String> itr=al.iterator();  
    while(itr.hasNext()){  
        System.out.print(itr.next()+" ");  
    } 

    System.out.println(" ");

    al.add("element4");  

    itr=al.iterator();  
    while(itr.hasNext()){  
        System.out.print(itr.next()+ " "); 
    } 
    System.out.println(" ");

    //pop_back
    al.pollLast();

    itr=al.iterator();  
    while(itr.hasNext()){  
        System.out.print(itr.next()+" ");  
      }  
    }  
}  

result:

结果:

element1 element2 element3  
element1 element2 element3 element4  
element1 element2 element3 

回答by Eklavya

Use ArrayListfor dynamically add and remove element.

使用ArrayList动态添加和删除元素。

For push_back()use .add()

push_back()使用.add()

add()method to add elements in the list

add()方法在列表中添加元素

List<Integer> arrlist = new ArrayList<Integer>(); 
arrlist.add(10); 

For pop_back()use .remove()

pop_back()使用.remove()

remove()used for removing the element at the specified position in this list. So, pass last index in method to delete last element

remove()用于删除此列表中指定位置的元素。因此,在方法中传递最后一个索引以删除最后一个元素

arrlist.remove(arrlist.size() - 1);