146

I don't know how to find the length of an array list. I think you might need to use blank.length(); but I'm not sure.

I made an array list

ArrayList<String> myList = new ArrayList<String>();

but how do I write code to print out the size of myList?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user1254044
  • 1,597
  • 3
  • 12
  • 5
  • 19
    int size = myList.size(); Pls try reading Javadoc and some tutorial on using Java collections. – anubhava Mar 11 '12 at 05:36
  • 8
    [Documentation](http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#size()) is your friend! – Andrew Marshall Mar 11 '12 at 05:39
  • 10
    It was probably more effort to ask the question here than to google it. – Kyle Mar 11 '12 at 05:39
  • 3
    @Kyle: Ah, but then you don't get any ego-boosting SO reputation points. – RenniePet Jul 29 '13 at 19:50
  • 9
    @Kyle, also this question is now the top hit on google for "ArrayList length" – Dave Branton Jan 27 '14 at 00:52
  • 15
    @Kyle: Much faster to google and read this post than to go through docs. – Floris Jan 28 '14 at 09:48
  • 1
    @Kyle - Hopefully you figured this out on your own in the past 4 years, but simple questions like this one often become the top result on Google years later. Their asking is hugely beneficial to future people searching for the answer (like myself). – ArtOfWarfare Mar 31 '16 at 20:57

2 Answers2

271

The size member function.

myList.size();

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Adam Reed
  • 3,294
  • 2
  • 15
  • 15
  • myList.size() gives actual no of elements in the list. Note that since indexing starts from 0, no of elements (according to indexing) will be size() - 1. – Vadiraj S J Apr 29 '19 at 07:11
41
System.out.println(myList.size());

Since no elements are in the list

output => 0

myList.add("newString");  // use myList.add() to insert elements to the arraylist
System.out.println(myList.size());

Since one element is added to the list

output => 1

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
Sergej Raishin
  • 535
  • 4
  • 7