์๋ฐ Map ์์ฃผ ์ฐ๋ ๋ฉ์๋ ์ ๋ฆฌ
๊ฐ ๋ฃ๊ธฐ & ๊ฐ์ ธ์ค๊ธฐ put() / get(key) Map map = new HashMap(); map.put("์์", 10); map.put("์ฑ
์", 5); map.put("๋ฉํฐํญ", 3); int val = 0; val = map.get("์์");// 10 val = map.get("์ฑ
์");// 5 val = map.get("๋ฉํฐํญ");// 3 ํค๊ฐ ์์ผ๋ฉด ๊ฐ ๋ฐํ, ์์ผ๋ฉด ๊ธฐ๋ณธ ๊ฐ ๋ฐํ getOrDefault(key, default) String[] alphabet = {"A", "B", "C" ,"A"}; Map map = new HashMap(); for (String key : alphabet) { map.put(key, map.getOrDefault(key, 0) + 1); } // ..
2021. 11. 2.
์๋ฐ ๋ฐฐ์ด, ๋ฆฌ์คํธ ์ ๋ ฌ (Array, List ์ ๋ ฌ)
๋ฐฐ์ด(Array) ์ ๋ ฌ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ int[] arr = {1, 2, 4, 5, 3}; Arrays.sort(arr); ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ int[] arr = {1, 2, 4, 5, 3}; Arrays.sort(arr, Collections.reverseOrder()); ์ผ๋ถ๋ง ์ ๋ ฌ int[] arr = {1, 2, 5, 4, 3};// 1 2 4 5 3 Arrays.sort(arr, 0, 4);// 1, 2, 4, 5 ์์๋ง ์ ๋ ฌ ๋ฆฌ์คํธ(List) ์ ๋ ฌ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ ArrayList list = new ArrayList(Arrays.asList("c", "a", "b")); Collections.sort(list);// a b c ArrayList list = new ArrayList(Arrays.as..
2021. 11. 2.