How to find the length of an array list?

I have this code
@Test public void MainArraylist(){ OpenMainMenu(); List<String> s = new ArrayList<String>(); s.add("LogIn"); s.add("Welcome"); int retval = s.size(); System.out.println("Size of list = " + retval); Iterator<String> itr = s.iterator(); Assert.assertTrue(itr.hasNext()); }
It’s clear . But how can I find out the length , if the list of a lot of elements ?

List<String> s = new ArrayList<String>();
        int retval = s.size();
        System.out.println("Size of list = " + retval);
        for ( Iterator<String> itr = s.iterator(); itr.hasNext();) {
            String tempString =;
            // do something with tempString

What’s wrong with just using that line of code to retrieve the length? Does not method not return the length of your list?

Size of list = 2

Default Suite
Total tests run: 1, Failures: 0, Skips: 0

But he announces the number of lines that I write .
I have an Android, there is a long list , and I need to know the length of this list, and not to declare each row .I newbie and do not really understand

I do not understand what you are asking. I currently have the following interpretations of your questions:

How does the size() method of the List interface work?

The size() method of the Java List interface is supposed to return the number of elements that are stored in the list, and the ArrayList class does just that. It does not return the number of lines of codes you write. For example, if I write

List<Integer> myList = new ArrayList<>();
for (int index = 0; index < 10; index++) {

then I should expect to see the number “10” printed to my console when I run this program, because there are 10 elements that have been added to the list.

In any case, this is not a Java help forum. If you would like more help with Java, I recommend picking up a book or attending some introductory courses to Java. Try completing a few projects so that you can familiarize yourself with the Java environment!

How do Android lists work?

Lists in Android are usually constructed with using ListView objects. ListViews actually use an adapter that is used to build the entry views that are shown in the list. The adapter also has access to a set of data used to build these entry views. This design allows the ListView class to be designed without being concerned about data or how the data should be presented. The adapter acts as the bridge between the data and the view.

One of the most interesting facts about the ListView is that the ListView only asks the adapter to generate views for data on demand. This means that if you’re adapter has 1000 data entries, but the Android screen only has enough room to show 10 at a time, then the ListView will only ask the adapter for 10 views for 10 data entries. The remaining 990 data items will not have Views built for them until the ListView has more room for them. This design saves memory (because the Views for the non-visible data items aren’t generated yet) and processing time (because the processor doesn’t waste cycles building Views that won’t be looked at immediately).

The effect of this approach for automation, however, is that whenever you look at the child views of a ListView, you will only see views for a portion of the backing data, and never the complete list. This is a rather big problem for Uiautomator automation frameworks (and Appium for Android runs on Uiautomator). Usually, the most practical solution or workaround for these types of problems is to implement an Android instrumented test that runs in the same process as the app under test; this allows the instrumented test to query the ListView’s adapter and the data directly rather than having to navigate through the UI to look at data. However, there are definitely cases where an automation framework would want to be able to distinguish views within a ListView, which is currently difficult for Appium to perform.