java中的getfirst_Java ConcurrentLinkedDeque getFirst()用法及代码示例

java.util.concurrent.ConcurrentLinkedDeque.getFirst()方法是Java中的内置方法,它返回双端队列容器的第一个元素。

用法:

Conn_Linked_Deque.getFirst()

参数:该方法不接受任何参数。

返回值:该方法返回双端队列中存在的第一个元素。

异常:当双端队列为空时,该函数将引发NoSuchElementException。

以下示例程序旨在说明ConcurrentLinkedDeque.getFirst()方法:

程序1:

/* Java Program to Demonstrate getFirst()

method of ConcurrentLinkedDeque */

import java.util.concurrent.*;

class GFG {

public static void main(String[] args)

{

// Creating an empty Deque

ConcurrentLinkedDeque cld =

new ConcurrentLinkedDeque();

// Add elements into the Deque

cld.add("Welcome");

cld.add("To");

cld.add("Geeks");

cld.add("4");

cld.add("Geeks");

// Displaying the Deque

System.out.println("Elements in the Deque: " + cld);

// Displaying the first element

System.out.println("The first element is: " +

cld.getFirst());

}

}

输出:

Elements in the Deque: [Welcome, To, Geeks, 4, Geeks]

The first element is: Welcome

示例2:

/* Java Program to Demonstrate getFirst()

method of ConcurrentLinkedDeque */

import java.util.concurrent.*;

class GFG {

public static void main(String[] args)

{

// Creating an empty Deque

ConcurrentLinkedDeque cld =

new ConcurrentLinkedDeque();

try {

// Displaying the first element

System.out.println("The first element "

+ "is: " + cld.getFirst());

}

catch (Exception e) {

System.out.println(e);

}

// Add elements into the Deque

cld.add(12);

cld.add(43);

cld.add(29);

cld.add(16);

cld.add(70);

// Displaying the Deque

System.out.println("Elements in the Deque: " + cld);

// Displaying the first element

System.out.println("The first element is: " +

cld.getFirst());

}

}

输出:

java.util.NoSuchElementException

Elements in the Deque: [12, 43, 29, 16, 70]

The first element is: 12

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值