0

有没有办法找出一个 ArrayList 拥有多少个列表?

ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>(); List<Integer> myOtherList = new LinkedList<Integer>();

4

2 回答 2

5

与任何列表一样,您可以通过 找出其中包含的对象数量List#size()

int size = myList.size(); // amount of sublists that myList holds
于 2012-12-02T00:04:49.907 回答
2

@Vulcan 的回复将为您提供列表的大小。如果您只想要列表中包含的 ArrayList,则需要使用 instanceof 检查元素类型并以这种方式增加计数:

int listcount = 0;
for (Object e : list) {
   if (e instanceof ArrayList) listcount++;
}
于 2012-12-02T00:12:30.280 回答