Java AbstractCollection 类中的 size() 方法
AbstractCollection 类的 size() 方法返回集合中的元素数。如果集合中的元素总数超过 Interger.MAX_VALUE,则该方法将返回 Integer.MAX_VALUE。
语法如下
public abstract int size()
要在 Java 中使用 AbstractCollection 类,请导入以下包
import java.util.AbstractCollection;
下面是一个在 Java 中实现 AbstractCollection size() 方法的示例
示例
import java.util.ArrayList;
import java.util.AbstractCollection;
public class Demo {
public static void main(String[] args) {
AbstractCollection<Object> absCollection = new ArrayList<Object>();
absCollection.add("Laptop");
absCollection.add("Tablet");
absCollection.add("Mobile");
absCollection.add("E-Book Reader");
absCollection.add("SSD");
absCollection.add("HDD");
System.out.println("AbstractCollection = " + absCollection);
System.out.println("Count of Elements in the AbstractCollection = " + absCollection.size());
absCollection.remove("Tablet");
absCollection.remove("SSD");
System.out.println("Collection after removing some 2 elements = " + absCollection);
System.out.println("Count of Elements in the updated AbstractCollection = " + absCollection.size());
}
}输出
AbstractCollection = [Laptop, Tablet, Mobile, E-Book Reader, SSD, HDD] Count of Elements in the AbstractCollection = 6 Collection after removing some 2 elements = [Laptop, Mobile, E-Book Reader, HDD] Count of Elements in the updated AbstractCollection = 4
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP