logo

Java 목록 크기() 메서드

그만큼 크기() List 인터페이스의 메서드는 이 목록에 있는 요소의 총 개수를 반환합니다.

jframe

통사론

 public int size () 

매개변수

저것

반품

size() 메소드는 이 목록의 요소 수를 반환합니다.

실시예 1

 import java.util.LinkedList; import java.util.List; public class JavaListSizeExample1 { public static void main(String[] args) { List list= new LinkedList(); list.add(34); list.add(56); list.add(12); list.add(43); System.out.println('List : '+list); //it will return the total size of the list int size= list.size(); System.out.println('Total element of the list : '+size); } } 
지금 테스트해보세요

산출:

 List : [34, 56, 12, 43] Total element of the list : 4 

실시예 2

 import java.util.LinkedList; import java.util.List; import java.util.ListIterator; class Student{ String name; String school; Student(String name, String school){ this.name=name; this.school=school; } public String toString() { return ' Name : '+this.name+' School : '+this.school; } } public class JavaListSizeExample2 { public static void main(String[] args) { List list = new LinkedList(); Student s1= new Student('Reema','Gita Convent School'); Student s2= new Student('Geetu','Gita Convent School'); Student s3= new Student('Saloni','Eicher School'); list.add(s1); list.add(s2); list.add(s3); //returns the total number of students in the list System.out.println('Total students : '+list.size()); for (Student val : list){ System.out.println(val); } } } 
지금 테스트해보세요

산출:

 Total students : 3 Name : Reema School : Gita Convent School Name : Geetu School : Gita Convent School Name : Saloni School : Eicher School 

실시예 3

 import java.util.LinkedList; import java.util.List; public class JavaListSizeExample3 { public static void main(String[] args) { List list= new LinkedList(); for (char ch=&apos;a&apos;;ch<='z';ch++){ list.add(ch); } system.out.println('total alphabets : '+list.size()); system.out.println('list '+list); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Total alphabets : 26 List : [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z] </pre></='z';ch++){>