📜  查找 ArrayList 的长度/大小的Java程序

📅  最后修改于: 2022-05-13 01:55:16.153000             🧑  作者: Mango

查找 ArrayList 的长度/大小的Java程序

给定Java中的 ArrayList,任务是编写Java程序来查找 ArrayList 的长度或大小。

例子:

Input: ArrayList: [1, 2, 3, 4, 5]
Output: 5

Input: ArrayList: [geeks, for, geeks]
Output: 3

ArrayList – ArrayList 是集合框架的一部分,存在于Java.util包中。它为我们提供了Java中的动态数组。但是,它可能比标准数组慢,但在需要对数组进行大量操作的程序中很有帮助。

方法——使用 size() 方法

可以借助 size() 方法轻松确定 ArrayList 的大小。此方法不接受任何参数,并返回一个整数值,即 ArrayList 的大小。

句法:

int size = ArrayList.size();

下面是上述方法的实现:

示例 1 – 确定 Integer ArrayList 大小的Java程序

Java
// Java program to find the size
// of an ArrayList
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Creating object of ArrayList
        ArrayList
            arrlist = new ArrayList();
  
        // Populating arrlist
        arrlist.add(1);
        arrlist.add(2);
        arrlist.add(3);
        arrlist.add(4);
        arrlist.add(5);
  
        // print arrlist
        System.out.println("ArrayList: "
                           + arrlist);
  
        // getting total size of arrlist
        // using size() method
        int size = arrlist.size();
  
        // print the size of arrlist
        System.out.println("Size of ArrayList = "
                           + size);
    }
}


Java
// Java program to find the size
// of an String ArrayList
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Creating object of ArrayList
        ArrayList
            arrlist = new ArrayList();
  
        // Populating arrlist
        arrlist.add("GeeksforGeeks");
        arrlist.add("a");
          arrlist.add("computer");
        arrlist.add("science");
          arrlist.add("portal");
        arrlist.add("for");
          arrlist.add("geeks");
  
        // print arrlist
        System.out.println("ArrayList: "
                           + arrlist);
  
        // getting total size of arrlist
        // using size() method
        int size = arrlist.size();
  
        // print the size of arrlist
        System.out.println("Size of ArrayList = "
                           + size);
    }
}


输出
ArrayList: [1, 2, 3, 4, 5]
Size of ArrayList = 5

示例 2 – 确定字符串 ArrayList 大小的Java程序

Java

// Java program to find the size
// of an String ArrayList
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Creating object of ArrayList
        ArrayList
            arrlist = new ArrayList();
  
        // Populating arrlist
        arrlist.add("GeeksforGeeks");
        arrlist.add("a");
          arrlist.add("computer");
        arrlist.add("science");
          arrlist.add("portal");
        arrlist.add("for");
          arrlist.add("geeks");
  
        // print arrlist
        System.out.println("ArrayList: "
                           + arrlist);
  
        // getting total size of arrlist
        // using size() method
        int size = arrlist.size();
  
        // print the size of arrlist
        System.out.println("Size of ArrayList = "
                           + size);
    }
}
输出
ArrayList: [GeeksforGeeks, a, computer, science, portal, for, geeks]
Size of ArrayList = 7