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

更新日期:30-7-2019

158 瀏覽

開啟您的 職業生涯

完成課程以獲得認證

入門
廣告
© . All rights reserved.