length, length(), size()

Gyujin Cho·2022년 2월 1일
0

Java

목록 보기
2/7

1. length

배열의 길이 출력.

public class gyujin {
	public static void main(String[] args) {
		int[] array = { 1, 2, 3 };
		int length = array.length;
		System.out.println(length);
	}
}
/* 출력결과
5
*/
public class gyujin {
	public static void main(String[] args) {
		int[] array = new int[10];
		int length = array.length;
		System.out.println(length);
	}
}
/* 출력결과
10
*/

배열의 크기를 10으로 초기화하였기때문에, 배열에 따로 원소를 넣지 않아도 10으로 출력된다.

2. length()

문자열의 길이 출력.

public class gyujin{
	public static void main(String[] args){
        String lengthTest2 = "lengthSizeTest";
        System.out.println( lengthTest2.length() );
    }
}
/* 출력결과
14
*/

3. size()

컬렉션프레임워크 타입의 길이 출력.

public class gyujin{
	public static void main(String[] args){
        ArrayList<Object> sizeTest = new ArrayList<Object>();
        sizeTest.add(10);
        sizeTest.add(20);
        System.out.println( sizeTest .size() );
    }
}
/* 출력결과
2
*/
profile
간단 명료하게

0개의 댓글