간단명료

length, length(), size() 본문

Java

length, length(), size()

FeelGoood 2022. 2. 16. 20:27

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
*/
728x90
반응형

'Java' 카테고리의 다른 글

List - Array 데이터 이동  (0) 2022.02.16
Pair  (0) 2022.02.16
ArrayList  (0) 2022.02.16
Math 클래스  (0) 2022.02.16
charAt()  (0) 2022.02.16
Comments