[JAVA] length, length(), size()의 차이

Soo·2024년 1월 29일
post-thumbnail

굉장히 단순하지만 가끔 헷갈려서 기록해두는 용으로 적어본다.

  • 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 = "I am soogineer";
		System.out.println(str.length()); //14

		//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("a", "candy");
		map.put("b", "chocolate");
		map.put("c", "beer");
		System.out.println(map.size()); //3
	}
profile
Soogineer's Devlog

0개의 댓글