예제가 포함된 Java의 List size() 메소드

예제가 포함된 Java의 List size() 메소드

그만큼 크기() 의 방법 목록 인터페이스 Java에서는 이 목록의 요소 수를 가져오는 데 사용됩니다. 즉, 목록 size() 메서드는 이 목록 컨테이너에 있는 요소의 개수를 반환합니다.

통사론:

public int size() 

매개변수 : 이 메서드는 매개 변수를 사용하지 않습니다.

반환 값: 이 메소드는 요소 수 이 목록에 있습니다.

Java의 목록 크기 방법

삽화: 정수 목록이라고 가정합니다.

 Input : {3,2,1,5,7} Output : 5 

예시 1:

자바




// 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);> > }> }>

산출

Before operation: [1, 2, 3, 4, 5] Size of list = 5 

예시 2:

자바




// 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);> > }> }>

산출

Before operation: [Geeks, for, Geeks] Size of list = 3