0

Is there away of finding out how many Lists an ArrayList holds?

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

2 Answers 2

5

As with any list, you can find out how many objects it is holding with List#size().

int size = myList.size(); // amount of sublists that myList holds
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, of course. I can't believe i didn't think of this. Thank you. :D
We all have those days, no worries ;) Just don't forget to mark the answer as accepted when you can.
2

@Vulcan's response will get you the size of the list. If you want only the ArrayLists contained within the List, you'll need to check the element type using instanceof and increment the count that way:

int listcount = 0;
for (Object e : list) {
   if (e instanceof ArrayList) listcount++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.