Java length, length(), size()의 차이

Sunset·2022년 6월 28일
0

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
	}

1개의 댓글

comment-user-thumbnail
2023년 9월 14일

감사합니다

답글 달기