IL misurare() metodo del Interfaccia elenco in Java viene utilizzato per ottenere il numero di elementi in questo elenco. Cioè, il metodo list size() restituisce il conteggio degli elementi presenti in questo contenitore di elenco.
int alla stringa C++
Sintassi:
public int size()>
Parametri : Questo metodo non accetta alcun parametro.
Valore di ritorno: Questo metodo restituisce il file numero di elementi in questo elenco.
Illustrazione: Supponiamo che sia una lista di numeri interi
Input : {3,2,1,5,7} Output : 5>
Esempio 1:
Giava
// Java program to Illustrate size() method> // of List class for Integer value> // Importing required classes> import> java.util.*;> // Main class> public> class> GFG {> > // Main driver method> > public> static> void> main(String[] arg)> > {> > // Creating object of ArrayList class> > List list => new> ArrayList();> > // Populating List by adding integer elements> > // using add() method> > list.add(> 1> );> > list.add(> 2> );> > list.add(> 3> );> > list.add(> 4> );> > list.add(> 5> );> > // Printing elements of List> > System.out.println(> 'Before operation: '> + list);> > // Getting total size of list> > // using size() method> > int> size = list.size();> > // Printing the size of List> > System.out.println(> 'Size of list = '> + size);> > }> }> |
>
>Produzione
Before operation: [1, 2, 3, 4, 5] Size of list = 5>
Esempio 2:
Giava
// Java program to Illustrate size() method> // of List class for Integer value> // Importing required classes> import> java.util.*;> // Main class> public> class> GFG {> > // Main driver method> > public> static> void> main(String[] args)> > {> > // Creating an empty string list by> > // declaring elements of string type> > List list => new> ArrayList();> > // Populating List by adding string elements> > // using add() method> > list.add(> 'Geeks'> );> > list.add(> 'for'> );> > list.add(> 'Geeks'> );> > // Printing the List> > System.out.println(> 'Before operation: '> + list);> > // Getting total size of list> > // using size() method> > int> size = list.size();> > // Printing the size of list> > System.out.println(> 'Size of list = '> + size);> > }> }> |
>
>Produzione
Before operation: [Geeks, for, Geeks] Size of list = 3>