當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Deque size()用法及代碼示例


Deque 接口的 size() 方法用於返回給定雙端隊列中存在的元素總數。

用法:

int size()

參數:

上述方法隻需要一個參數:

  1. 需要移除的元素 '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 size() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。