length, length(), size() 가 헷갈릴꺼 같아 끄적여 둔다.
length : 배열의 길이 알려 할 때
length() : 문자열의 길이를 알려 할 때
size() : Collection, 자료구조의 크기를 알려 할 때
public static void main(String[] args) {
//Array
int [] arr = new int[11];
System.out.println(arr.length); //11
//String
String str = "세상에서 제일가는 토테이토칩";
System.out.println(str.length()); //15
//ArrayList
ArrayList<Object> list = new ArrayList<Object>();
list.add(0, "123");
list.add(1, "234");
System.out.println(list.size()); //2
//HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put("가", "초콜릿");
map.put("나", "비빔면");
map.put("다", "제로콜라");
System.out.println(map.size()); //3
}
감사합니다