如何在Java中查找arraylist的长度/大小
时间:2020-02-23 14:34:14 来源:igfitidea点击:
在本教程中,我们将看到如何在Java中查找ArrayList的长度/大小。
我们可以使用ArrayList'size()方法计算ArrayList的大小。
让我们在简单的例子的帮助下了解它。
package com.igi.theitroad;
import java.util.ArrayList;
import java.util.List;
public class ArrayListLengthMain {
public static void main(String args[])
{
List<String> countryList=new ArrayList<>();
countryList.add("Netherlands");
countryList.add("China");
countryList.add("Bhutan");
countryList.add("Nepal");
System.out.println("Size of Country List is: "+countryList.size());
System.out.println(countryList);
countryList.remove(1);
countryList.remove(2);
System.out.println("Size of Country List is: "+countryList.size());
System.out.println(countryList);
}
}
运行上面的程序时,我们将得到以下输出:
Size of Country List is: 4 [Netherlands, China, Bhutan, Nepal] Size of Country List is: 2 [Netherlands, Bhutan]
如我们所见在此处,ArrayList的大小为4,然后我们从中删除了两个元素,ArrayList的大小变为两个。

