컴퓨터공학 💻 도서관📚

자바 length, length(), size()의 차이 본문

💻☕프로그래밍 언어/Java

자바 length, length(), size()의 차이

들판속초록풀 2025. 4. 23. 10:55

length배열의 길이를 알고 싶을 때

length()문자열의 길이를 알고 싶을 때

size()Collection, 자료구조의 크기를 알고 싶을 때

 

public static void main(String[] args) {

		//Array
		int [] arr = new int[11];
		System.out.println(arr.length);   // 1

		//String
		String str = "세상에서 제일가는 토테이토칩";
		System.out.println(str.length());  // 2



		//ArrayList
		ArrayList<Object> list = new ArrayList<Object>();
		list.add(0, "123");
		list.add(1, "234");
		System.out.println(list.size());  // 3-1

		//HashMap
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("가", "초콜릿");
		map.put("나", "비빔면");
		map.put("다", "제로콜라");
		System.out.println(map.size());   // 3-2
	}

 

 

 

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

 

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

length, length(), size() 가 헷갈릴꺼 같아 끄적여 둔다.length : 배열의 길이 알려 할 때 length() : 문자열의 길이를 알려 할 때size() : Collection, 자료구조의 크기를 알려 할 때

velog.io

 

Comments