PHP版本要求: 7.0.0 或更高版本
用法:
isEmpty(): bool
isEmpty()方法用於檢查集合是否為空。如果集合中沒有元素,則返回true;否則,返回false。
示例:
use Ds\Collection;
$collection = new \Ds\Vector();
echo $collection->isEmpty(); // 輸出 true
$collection->push(1);
echo $collection->isEmpty(); // 輸出 false
$emptySet = new \Ds\Set();
echo $emptySet->isEmpty(); // 輸出 true
$emptyMap = new \Ds\Map();
echo $emptyMap->isEmpty(); // 輸出 true
在以上示例中,我們建立了一個空的Vector集合,並使用isEmpty()方法檢查它是否為空。由於集合中沒有任何元素,因此isEmpty()方法返回true。
然後,我們將一個元素1新增到Vector集合中,並再次使用isEmpty()方法進行檢查。現在,集合不再為空,因此isEmpty()方法返回false。
接下來,我們建立了一個空的Set集合和一個空的Map集合,並使用isEmpty()方法進行檢查。由於這兩個集合都沒有任何元素,所以isEmpty()方法對它們返回true。
請注意,isEmpty()方法對於所有實現了Ds\Collection介面的集合類(如Vector、Set和Map)都可用。
熱門工具排行榜