Recent Posts
간단명료
length, length(), size() 본문
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
반응형
Comments