Deque 接口的 size() 方法用于返回给定双端队列中存在的元素总数。
用法:
int size()
参数:
上述方法只需要一个参数:
- 需要移除的元素 'o'。
指定者:
接口 Collection<E> 中的大小
返回:
上述方法用于返回给定双端队列中存在的元素总数。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeresizeExample1 {
public static void main(String[] args) {
Deque<Float> deque = new ArrayDeque<>();
deque.add(55.8f);
deque.add(22.3f);
deque.add(89f);
deque.add(22.3f);
System.out.println("The final deque is given as:");
for (Float float1:deque) {
System.out.println(float1);
}
int size = deque.size();
System.out.println("The size of the deque is:"+size);
}
}
输出:
The final deque is given as: 55.8 22.3 89.0 22.3 The size of the deque is:4
例子2
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeresizeExample2 {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.add("Computer");
deque.add("Programming");
deque.add("Language");
System.out.println("The final deque is given as:");
for (String string:deque) {
System.out.println(string);
}
int size = deque.size();
System.out.println("The size of the deque is:"+size);
}
}
输出:
The final deque is given as: Computer Programming Language The size of the deque is:3
例子3
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeresizeExample3 {
public static void main(String[] args) {
Deque<Character> deque = new ArrayDeque<>();
deque.add('A');
deque.add('B');
deque.add('C');
deque.add('D');
deque.add('E');
System.out.println("The final deque is given as:"+deque);
int size = deque.size();
System.out.println("The size of the deque is:"+size);
}
}
输出:
The final deque is given as:[A, B, C, D, E] The size of the deque is:5
相关用法
- Java Deque pollLast()用法及代码示例
- Java Deque remove()用法及代码示例
- Java Deque removeLast()用法及代码示例
- Java Deque addLast()用法及代码示例
- Java Deque addAll()用法及代码示例
- Java Deque poll()用法及代码示例
- Java Deque pop()用法及代码示例
- Java Deque pollFirst()用法及代码示例
- Java Deque getLast()用法及代码示例
- Java Deque getFirst()用法及代码示例
- Java Deque iterator()用法及代码示例
- Java Deque peek()用法及代码示例
- Java Deque add()用法及代码示例
- Java Deque removeFirstOccurrence()用法及代码示例
- Java Deque offerFirst()用法及代码示例
- Java Deque addFirst()用法及代码示例
- Java Deque offer()用法及代码示例
- Java Deque element()用法及代码示例
- Java Deque descendingIterator()用法及代码示例
- Java Deque peekLast()用法及代码示例
注:本文由纯净天空筛选整理自 Java Deque size() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。