java给数组push_Java ArrayDeque push()用法及代码示例

Java.util.ArrayDeque.push(E element)方法用于将元素推送到Deque中。该操作类似于堆栈中的操作。元素被推到双端队列的顶部。

用法:

Array_Deque.push(E element)

参数:参数element 的类型与ArrayDeque中元素相同,表示要推入双端队列的元素。

返回值:该方法不返回任何值。

异常:如果传递的参数为NULL,则该方法将引发NullPointerException。

下面的程序演示了Java.util.ArrayDeque.push()方法:

示例1:将字符串元素添加到双端队列。

// Java code to illustrate push()

import java.util.*;

public class ArrayDequeDemo {

public static void main(String args[])

{

// Creating an empty ArrayDeque

Deque de_que = new ArrayDeque();

// Use add() method to add elements into the Deque

de_que.add("Welcome");

de_que.add("To");

de_que.add("Geeks");

de_que.add("4");

de_que.add("Geeks");

// Displaying the initial ArrayDeque

System.out.println("Initial Deque: " + de_que);

// Pushing elements into the deque

de_que.push("Hello");

de_que.push("World");

// Displaying the final ArrayDeque

System.out.println("Final Deque: " + de_que);

}

}

输出:

Initial Deque: [Welcome, To, Geeks, 4, Geeks]

Final Deque: [World, Hello, Welcome, To, Geeks, 4, Geeks]

示例2:将Integer元素添加到双端队列中。

// Java code to illustrate push()

import java.util.*;

public class ArrayDequeDemo {

public static void main(String args[])

{

// Creating an empty ArrayDeque

Deque de_que = new ArrayDeque();

// Use add() method to add elements into the Deque

de_que.add(10);

de_que.add(15);

de_que.add(30);

de_que.add(20);

de_que.add(5);

// Displaying the initial ArrayDeque

System.out.println("Initial Deque: " + de_que);

// Pushing elements into the deque

de_que.push(1254);

de_que.push(4521);

// Displaying the final ArrayDeque

System.out.println("Final Deque: " + de_que);

}

}

输出:

Initial Deque: [10, 15, 30, 20, 5]

Final Deque: [4521, 1254, 10, 15, 30, 20, 5]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值