有没有办法找出一个 ArrayList 拥有多少个列表?
ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
List<Integer> myOtherList = new LinkedList<Integer>();
与任何列表一样,您可以通过 找出其中包含的对象数量List#size()
。
int size = myList.size(); // amount of sublists that myList holds
@Vulcan 的回复将为您提供列表的大小。如果您只想要列表中包含的 ArrayList,则需要使用 instanceof 检查元素类型并以这种方式增加计数:
int listcount = 0;
for (Object e : list) {
if (e instanceof ArrayList) listcount++;
}