Java中的 AbstractSequentialList size() 方法与示例
Java中AbstractSequentialList的size()方法用于获取此 AbstractSequentialList 实例的大小。它返回一个整数值,该值是此 AbstractSequentialList 实例的大小。
句法:
public int size()
参数:此方法不接受任何参数。
返回:该方法返回一个整数值,该值是此 AbstractSequentialList 实例的大小。
下面的示例说明了 AbstractSequentialList.size() 方法:
示例 1:
// Java code to demonstrate the working of
// size() method in AbstractSequentialList
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// creating an AbstractSequentialList
AbstractSequentialList arr
= new LinkedList();
// using add() to initialize values
// [1, 2, 3, 4]
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
// print AbstractSequentialList
System.out.println("AbstractSequentialList: "
+ arr);
// Get the size
// using size()
System.out.println("Size: "
+ arr.size());
}
}
输出:
AbstractSequentialList: [1, 2, 3, 4]
Size: 4
示例 2:
// Java code to demonstrate the working of
// size() method in AbstractSequentialList
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// creating an AbstractSequentialList
AbstractSequentialList arr
= new LinkedList();
// using add() to initialize values
// [Geeks, For, ForGeeks, GeeksForGeeks]
arr.add("Geeks");
arr.add("For");
arr.add("ForGeeks");
arr.add("GeeksForGeeks");
// print AbstractSequentialList
System.out.println("AbstractSequentialList: "
+ arr);
// Get the size
// using size()
System.out.println("Size: "
+ arr.size());
}
}
输出:
AbstractSequentialList: [Geeks, For, ForGeeks, GeeksForGeeks]
Size: 4