队列,一种结构体,遵循先进先出的原则。
java中提供的方法有以下几种:
操作有插入、删除和检查某个下标是否有值三种,其中每种操作包含两个函数,它们的功能是相同的,区别在于insert,remove,element三个函数在操作失败是报异常,而offer,poll,peek三个函数不报异常。
具体描述原句是这样的:
Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation).
但是这种说法并不绝对,poll和peek确实不报异常,但是offer函数在某些情况下会报异常,具体如下:
ClassCastException - if the class of the specified element prevents it from being added to this queueNullPointerException - if the specified element is null and this queue does not permit null elements
IllegalArgumentException - if some property of this element prevents it from being added to this queue
但是插入发生异常的情况可能性比较小。
在以后的使用中,要根据应用场景选择合适的函数。