๐ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ
| import java.util.*; |
| import java.io.*; |
๐ค ์
๋ ฅ
| Scanner sc = new Scanner(System.in); |
| int n = sc.nextInt(); |
| int[][] times = new int[n][2]; |
| for (int i = 0; i < n; i++) { |
| times[i][0] = sc.nextInt(); |
| times[i][1] = sc.nextInt(); |
| } |
| String s = sc.next(); |
| |
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| int num = Integer.parseInt(br.readLine()); |
| int[][] points = new int[num][2]; |
| StringTokenizer st; |
| for(int i = 0; i < num; i++){ |
| st = new StringTokenizer(br.readLine(), " "); |
| points[i][0] = Integer.parseInt(st.nextToken()); |
| points[i][1] = Integer.parseInt(st.nextToken()); |
| } |
๐ค Character ๊ด๋ จ ๋ฉ์๋
| String str = "abcde"; |
| Character.isLetter(str.charAt(1)) |
| Character.isDigit(str.charAt(1)) |
| Character.isLetterOrDigit(str.cahrAt(1)) |
| Character.isAlphabetic(str.charAt(1)) |
| |
| |
| Character.isLowerCase('a') |
| Character.isUpperCase('A') |
| |
| |
| Character.toUpperCase('a') |
| Character.toLowerCase('A') |
| |
| |
| char c = '1'; |
| int n = Character.getNumericValue(c) |
๐ค ์์คํค ์ฝ๋
| |
| ๋๋ฌธ์ 'A': 65 ~ 'Z': 90, ์๋ฌธ์ 'a': 97 ~ 'z': 122 |
| ๋์๋ฌธ์ ์ฐจ์ด๋ 32 -> ์๋ฌธ์ - 32 = ๋๋ฌธ์ |
| |
| |
| '0' : 48 ~ '9': 57 |
| |
| |
| public int solution(String str){ |
| int answer = 0; |
| for(char x : str.toCharArray()){ |
| if( x >= '0' && x <= '9') answer = answer * 10 + (x-'0'); |
| |
| } |
| return answer; |
| } |
| |
| |
| char c = '1'; |
| int n = c; |
| |
| |
| int n = 49; |
| char c = n; |
| |
| |
| int a = 3; |
| char b = a + '0'; |
| |
| |
| int c = b - '0' |
๐ค String ๊ด๋ จ ๋ฉ์๋
| String str = "abcde"; |
| |
| str.length() |
| str.isEmpty() |
| |
| str.charAt(2) |
| str.indexOf("c") |
| str.lastIndexOf("c") |
| |
| str.substring(2, 4) |
| str.substring(3) |
| |
| str.replace('b', 'k') |
| str.replaceAll(๋ณํํ๊ณ ์ ํ๋ ๋ฌธ์์ด ๋๋ ์ ๊ท์, ๋ณํํ ๋ฌธ์ ๊ฐ) |
| str.replaceAll("[^A-Z]", "") |
| |
| str.equals("abcde") |
| str.equalsIgnoreCase("ABcde") |
| str.contains("bc") |
| |
| str.trim() |
| |
| str.toLowerCase() |
| str.toUpperCase() |
| |
| |
| |
| |
| |
| |
| |
| str.compareTo("abcdd") |
| |
| |
| String a = 3 + "" ; |
| String a = '3' + ""; |
| |
| |
| Integer.parseInt("300") |
| Integer.parseInt("10011", 2) |
| |
| |
| Integer.toString(300) |
| String.valueOf(300) |
| char[] s = {'a','b','c'} |
| String.valueOf(s) |
| |
| |
| String str = "12345"; |
| String[] Arr = str.split(""); |
| |
| |
| char[] charArr = str.toCharArray(); |
๐ค ์ ๊ท์
| [A-Z] |
| [a-z] |
| [^A-Z] |
| |
| [0-9] |
| [^0-9] |
๐ค StringBuilder ๊ด๋ จ ๋ฉ์๋
String์ ํ ๋ฒ ๋ง๋ค์ด์ง๋ฉด ๋ฌธ์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ ์ ์๋ ๋ณ๊ฒฝ ๋ถ๊ฐํ ํ์
, StringBuilder๋ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ํ์
์ด๋ฏ๋ก ๋ฌธ์์ด์ ๋ณ๊ฒฝํด์ผ ํ๋ ๋ฌธ์ ์์๋ StringBuilder๋ฅผ ์ฌ์ฉํ๋ค. (String Buffer๋ ๋๊ธฐํ ๊ฐ๋ฅ StringBuilder๋ ๋ถ๊ฐ๋ฅ. ๋๊ธฐํ๊ฐ ํ์ ์์ ์ StringBuilder๋ฅผ ์ฌ์ฉ ํ๋ ๊ฒ์ด ๋ ํจ์จ์ )
| StringBuilder sb = new StringBuilder(); |
| StringBuilder sb2 = new StringBuilder("abc"); |
| |
| sb.append("abc") // ๋ฌธ์์ด ์ถ๊ฐ |
| sb.insert(2, "kk") // 2 ์์น์ kk ์ฝ์
(โ abkkc) |
| |
| sb.delete(0, 2) // 0~1 ์์น์ ๋ฌธ์์ด ์ญ์ (โ c) |
| sb.deleteCharAt(2) // 2 ์์น์ ๋ฌธ์ ์ญ์ (โ ac) |
| |
| sb.setCharAt(0, 'h') // 0 ์์น์ ๋ฌธ์๋ฅผ h๋ก ๋ณ๊ฒฝ (โ hbc) |
| |
| sb.reverse() // ๋ฌธ์์ด ๊ฑฐ๊พธ๋ก ๋ค์ง๊ธฐ (โ cba) |
| |
| sb.setLength(2) // ๋ฌธ์์ด ๊ธธ์ด๋ฅผ 2๋ก ์ค์ (โ ab) |
| sb.setLength(4) // ๋ฌธ์์ด ๊ธธ์ด๋ฅผ 4๋ก ๋๋ฆผ (โ ๋ค๊ฐ ๊ณต๋ฐฑ์ผ๋ก ์ฑ์์ง) |
| |
| sb.toString() // String์ผ๋ก ๋ณํ |
๐ค Math
| |
| Math.max(10, 2); |
| Math.min(10, 2); |
| |
| |
| Math.abs(); |
| |
| |
| Math.ceil(-3.2); |
| Math.floor(-3.2); |
| Math.round(-3.26); |
| |
| |
| double a = 1.23456; |
| String b = String.format("%.1f", a); |
| |
| |
| double a = Math.pow(2, 3); |
| int a = (int) Math.pow(2, 3); |
| Math.sqrt(4); |
๐ค Array ๊ด๋ จ ๋ฉ์๋
| String[] arr1 = new String[5]; |
| int[] arr2 = {1, 2, 3}; |
| |
| int N = 3; |
| int[] arr3 = new int[N]; |
| |
| int arr[] = {10, 8, 11, 2, 3, 0}; |
| |
| |
| List list = Arrays.asList(arr1); |
| ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr1)); |
| |
| |
| int tmp[] = Arrays.copyOfRange(arr1, 0, 3); |
| |
| |
| Arrays.sort(arr1); |
| |
| |
| Arrays.sort(arr1, Collections.reverseOrder()); |
| |
| |
| Arrays.sort(arr1, 0, 4) |
| |
| |
| Arrays.binarySearch(arr1, 2); |
| |
| |
| Arrays.sort(times, new Comparator<int[]>() { |
| |
| @Override |
| public int compare(int[] o1, int[] o2) { |
| if(o1[1] == o2[1]) return o1[0] - o2[0]; |
| else return o1[1] - o2[1]; |
| } |
| }); |
| |
| |
| |
| |
| |
| |
| static void sort(Object[] objArr, Comparator c){ |
| for(int i = 0; i < objArr.length-1; i++){ |
| for(int j = 0; j < objArr.length-1; j++){ |
| Object tmp = null; |
| if(c.compare(objArr[j], objArr[j+1]) > 0){ |
| tmp = objArr[j]; |
| objArr[j] = objArr[j+1]; |
| objArr[j+1] = tmp; |
| } |
| } |
| } |
| } |
| |
| |
| int[] tmp = arr.clone(); |
| |
| |
| int max = Arrays.stream(arr).max().getAsInt(); |
| |
| |
| int min = Arrays.stream(arr).min().getAsInt(); |
| |
| |
| int sum = Arrays.stream(arr).sum(); |
๐ค ArrayList ๊ด๋ จ ๋ฉ์๋
| ArrayList<String> list = new ArrayList<String>(); |
| |
| list.add("์์ธ") |
| list.add(1, "๋์ ") |
| list.addAll(list2) |
| |
| list.get(0) |
| list.set(0, "๋๊ตฌ") |
| |
| list.indexOf("๋๊ตฌ") |
| list.lastIndexOf("๋๊ตฌ") |
| |
| list.remove(0) |
| list.remove("๋๊ตฌ") |
| list.removeAll(list2) |
| list.retainAll(list2) |
| |
| list.clear() |
| list.isEmpty() |
| list.size() |
| |
| list.contains("์์ธ") |
| list.containsAll(list2) |
| |
| list.removeIf(k -> k % 2 != 0) |
| |
| String[] temp = "abcde"; |
| ArrayList<String> list = new ArrayList<>(Arrays.asList(temp)); |
| |
| |
| ArrayList<String> list = new ArrayList<>(); |
| String[] temp = list.toArray(new String[list.size()]); |
| |
| |
| int[] temp = { 1123, 1412, 23, 44, 512132 }; |
| ArrayList<Integer> list = new ArrayList<>(Arrays.asList(temp)); |
| |
| |
| ArrayList<Integer> list = new ArrayList<>(); |
| int[] temp = list.stream().mapToInt(i->i).toArray(); |
๐ค Collections ๊ด๋ จ ๋ฉ์๋
| int[] arr = { 1123, 1412, 23, 44, 512132 }; |
| ArrayList<Integer> list = new ArrayList<>(Arrays.asList(arr)); |
| |
| Collections.max(list) |
| Collections.min(list) |
| |
| Collections.sort(list) |
| Collections.sort(list, Collections.reverseOrder()) |
| |
| Collections.reverse(list) |
| |
| Collections.frequency(list, 23) |
| |
| Collections.binarySearch(list, 44) |
| |
| |
๐ค Stack
| Stack<Integer> stack = new Stack<>(); |
| |
| stack.push(1) |
| stack.pop() |
| stack.clear() |
| stack.size() |
| stack.isEmpty() |
| stack.contains(1) |
| stack.peek() |
| stack.get(0) |
๐ค Queue
| Queue<Integer> queue = new LinkedList<>(); |
| |
| queue.add(1) |
| queue.offer(2) |
| queue.poll() |
| queue.remove() |
| queue.clear() |
| queue.peek() |
| queue.contains(1) |
๐ค PriorityQueue
| PriorityQueue<Integer> pq = new PriorityQueue<>(); |
| |
| |
| |
| pq.add(1) |
| pq.offer(1) |
| pq.poll() |
| pq.remove() |
| pq.clear() |
| pq.peek() |
๐ค HashSet
์ค๋ณต ํ์ฉํ์ง ์์ ๋๋ Set ์ฌ์ฉ
- HashSet : ์ค๋ณต์ ํ์ฉํ์ง ์๋ ๊ตฌ์กฐ. ์์๊ฐ ์๊ณ ์ ๋ ฌ๋ ์ ํจ.
- LinkedHashSet : ์ค๋ณต์ ํ์ฉํ์ง ์๋ ๊ตฌ์กฐ. ์ฝ์
๋ ์์๋๋ก ์์๋ฅผ ๊ด๋ฆฌํจ.
- TreeSet : ์ค๋ณต์ ํ์ฉํ์ง ์๋ ๊ตฌ์กฐ. ์ด์งํ์ํธ๋ฆฌ ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ฏ๋ก ์ ๋ ฌ ํจ.
| HashSet<Integer> set = new HashSet<>(); |
| |
| set.add(1) |
| set.remove(1) |
| set.removeAll(set2) |
| set.retainAll(set2) |
| set.clear() |
| set.size() |
| set.contains(1) |
| |
| |
| |
| Iterator iter = set.iterator(); |
| while (iter.hasNext()) |
| System.out.println(iter.next()); |
| |
| |
| for (String item: set) |
| System.out.println(item); |
๐ค TreeSet
์ค๋ณต ํ์ฉํ์ง ์๊ณ ์ ๋ ฌ ํ์ํ ๋ ์ฌ์ฉ
| |
| TreeSet<Integer> ts = new TreeSet<>(); |
| TreeSet<Integer> ts = new TreeSet<>(Collections.reverseOrder()); |
| |
| |
| ts.add(5); |
| ts.add(2); |
| ts.add(1); |
| ts.add(3); |
| |
| |
| ts.first(); |
| ts.last(); |
| |
| |
| ts.remove(5); |
| ts.pollFirst(); |
| ts.pollLast(); |
| ts.clear(); |
| |
| |
| ts.size() |
| |
| |
| 1. iterator ์ฌ์ฉ |
| 2. for-each ๋ฌธ์ผ๋ก ์์์ ์ ๊ทผ |
๐ค HashMap
- HashMap: <key, value>์. ํน์ ๊ท์น ์์ด ์ถ๋ ฅ๋จ.
- LinkedHashMap: <key, value>์. ํค๊ฐ์ด ์
๋ ฅ์์ผ๋ก ์ ๋ ฌ๋์ด ์ถ๋ ฅ๋จ.
- TreeMap: <key, value>์. ํค๊ฐ์ด ์ํ๋ฒณ์(์ค๋ฆ์ฐจ์)์ผ๋ก ์ ๋ ฌ๋ ์ํ๋ก ์ถ๋ ฅ๋จ.
| |
| HashMap<Integer, String> map = new HashMap<>(); |
| HashMap<String, String> map = new HashMap<>(); |
| |
| |
| map.put(1, "์ฌ๊ณผ") |
| map.put(2, "๋ฐ๋๋") |
| map.put(1, "ํฌ๋") |
| |
| |
| |
| |
| |
| |
| hm.put(key, hm.getOrDefault(key,0)+1) |
| |
| |
| map.get(1) |
| |
| |
| map.remove(1) |
| map.clear() |
| |
| |
| map.containsKey(1) |
| map.containsValue("์ฌ๊ณผ") |
| |
| |
| map.size() |
| |
| |
| map1.equals(map2) |
| |
| |
| |
| for (Integer key: map.keySet()) |
| System.out.println(i + map.get(key)); |
| |
| |
| for (Entry<Integer, String> entry: map.entrySet()) |
| System.out.println(entry.getKey() + entry.getValue()); |
source
https://velog.io/@hygge/Java-%EC%BD%94%EB%94%A9%ED%85%8C%EC%8A%A4%ED%8A%B8-%EB%AC%B8%EB%B2%95-%EC%86%8D%EC%84%B1-%EC%A0%95%EB%A6%AC
[Java] ์ฝ๋ฉํ
์คํธ ๋ฌธ๋ฒ ์์ฑ ์ ๋ฆฌ (1)
ํ์ด์ฌ์ผ๋ก ์ฝํ
๋ฅผ ๊ณต๋ถํ ๋..โ โฟโ ์ด๋ ๋ ์๋ฐ๋ก ์ฝ๋ฉํ
์คํธ๋ฅผ ์น๋ผ๋ ์ฒญ์ฒ๋ฒฝ๋ ฅ๊ฐ์ ๊ณต์ง๋ฅผ ๋ณด๊ฒ ๋๋๋ฐ.. (๋๋ณด๊ธฐ)
velog.io